kyupita Posted February 14 Share Posted February 14 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) (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! Link to comment https://forums.kleientertainment.com/forums/topic/169800-custom-item-randomly-spawning-in-world/ Share on other sites More sharing options...
Haruhi Kawaii Posted February 15 Share Posted February 15 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) (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. 1 Link to comment https://forums.kleientertainment.com/forums/topic/169800-custom-item-randomly-spawning-in-world/#findComment-1851566 Share on other sites More sharing options...
kyupita Posted February 17 Author Share Posted February 17 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 ^^ 1 Link to comment https://forums.kleientertainment.com/forums/topic/169800-custom-item-randomly-spawning-in-world/#findComment-1851857 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now