Jump to content

Recommended Posts

While playing with one of my character mods, me and a few other people found a weird bug. A custom item ("dnadata") was made to only drop (with a 10% chance) when a hostile mob with at least 50 health was killed. The code works as intended, but has a side effect of making a stack of this item spawn randomly somewhere in the world.

(The photos below aren't mine, they were sent to me)

DD1.png.21daba6ebd65758c9d95b1207af6ebf3.png

DD2.png.e5a09bcbc637fa07e56565849f7bc248.png

(The green circle is apparently the spawn point of the item)

 

I'd like to fix the issue since it's used to craft other items, and through my own time playing with the mod and encountering the bug, it's made certain fights way easier than intended.

This is the code I currently have for it to drop:

	-- Spawn "data" when mob is killed
	local function dropchance()
		return math.random(1, 10) -- 10% chance
	end
	
	local function IsValidVictim(victim)
		return victim ~= nil
			and not ((victim:HasTag("prey") and not victim:HasTag("hostile")) or
			victim:HasTag("veggie") or
			victim:HasTag("structure") or
			victim:HasTag("wall") or
			victim:HasTag("companion"))
			and victim.components.health ~= nil
			and victim.components.combat ~= nil
	end
	
	local function DropDataItem(inst, data)
		local v = data.victim
		local victim = data.victim
		local x, y, z = v.Transform:GetWorldPosition()

		local item = SpawnPrefab("dnadata")
		if IsValidVictim(victim) and
		v.components.health.maxhealth >= 50 and
		dropchance() == 1 then
			item.Transform:SetPosition(x, y, z)
			LaunchAt(item, v, inst, -0.5)
		end
	end

	inst:ListenForEvent("killed", DropDataItem)

Any help would be greatly appreciated!

14 hours ago, kyupita said:

While playing with one of my character mods, me and a few other people found a weird bug. A custom item ("dnadata") was made to only drop (with a 10% chance) when a hostile mob with at least 50 health was killed. The code works as intended, but has a side effect of making a stack of this item spawn randomly somewhere in the world.

(The photos below aren't mine, they were sent to me)

DD1.png.21daba6ebd65758c9d95b1207af6ebf3.png

DD2.png.e5a09bcbc637fa07e56565849f7bc248.png

(The green circle is apparently the spawn point of the item)

 

I'd like to fix the issue since it's used to craft other items, and through my own time playing with the mod and encountering the bug, it's made certain fights way easier than intended.

This is the code I currently have for it to drop:

	-- Spawn "data" when mob is killed
	local function dropchance()
		return math.random(1, 10) -- 10% chance
	end
	
	local function IsValidVictim(victim)
		return victim ~= nil
			and not ((victim:HasTag("prey") and not victim:HasTag("hostile")) or
			victim:HasTag("veggie") or
			victim:HasTag("structure") or
			victim:HasTag("wall") or
			victim:HasTag("companion"))
			and victim.components.health ~= nil
			and victim.components.combat ~= nil
	end
	
	local function DropDataItem(inst, data)
		local v = data.victim
		local victim = data.victim
		local x, y, z = v.Transform:GetWorldPosition()

		local item = SpawnPrefab("dnadata")
		if IsValidVictim(victim) and
		v.components.health.maxhealth >= 50 and
		dropchance() == 1 then
			item.Transform:SetPosition(x, y, z)
			LaunchAt(item, v, inst, -0.5)
		end
	end

	inst:ListenForEvent("killed", DropDataItem)

Any help would be greatly appreciated!

When this line of code runs:  local item = SpawnPrefab("dnadata") it means that dnadata has already been spawned somewhere in the world. You can use c_gonext("dnadata") to check its position. In your code, any entity that dies will drop dnadata, but only entities with a value greater than or equal to 50 will move the dnadata to their death position.

  • Thanks 1
On 2/15/2026 at 11:57 AM, Haruhi Kawaii said:

When this line of code runs:  local item = SpawnPrefab("dnadata") it means that dnadata has already been spawned somewhere in the world. You can use c_gonext("dnadata") to check its position. In your code, any entity that dies will drop dnadata, but only entities with a value greater than or equal to 50 will move the dnadata to their death position.

sorry I just got around to this, thank you so much! It seems like all I had to do was move "local item = SpawnPrefab("dnadata")" to a different spot and it got fixed ^^

  • Health 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...