Help with loot drop code

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    im trying to make a zombie drop certain items when it dies
  2. What is the issue? Include screenshots / videos if possible!
    it works only for the first two items and the rest of the code doesnt seems to do anything
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    tried setting the cframe or setting the position and something is wrong. also tried to place items in server storage
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local tn = script.Parent.Parent;
local dropitems = 
	{"cash","mediuma","heavya","cash","corpse","cash","corpse","corpse","lighta","corpse"}

local chosen = dropitems[math.random(1,#dropitems)]

script.Parent.Died:connect(function()
	
	if chosen == "cash" then
		local item = game.ReplicatedStorage.Cash10:Clone()
		item.Parent = game.Workspace--place item parent
		item.Position = script.Parent.Parent.UpperTorso.Position
	
	
	elseif chosen == "corpse" then
		wait(5)
		local item = game.ReplicatedStorage.Corpse:Clone()
		item.Parent = game.Workspace--place item parent
		item:SetPrimaryPartCFrame(script.Parent.Parent.UpperTorso.CFrame)
	
	
	elseif chosen == "lighta" then
		local item = game.ReplicatedStorage.smallammo.Clone()
		item.Parent = game.Workspace--place item parent
		item.Position = script.Parent.Parent.UpperTorso.Position
	
		
	elseif chosen == "mediuma" then
		local item = game.ReplicatedStorage.MediumAmmo.Clone()
		item.Parent = game.Workspace--place item parent
		item:SetPrimaryPartCFrame(script.Parent.Parent.UpperTorso.CFrame)
	
		
	elseif chosen == "heavya" then
		local item = game.ReplicatedStorage.LargeAmmo:Clone()
		item.Parent = game.Workspace--place item parent
		item.Position = script.Parent.Parent.UpperTorso.Position
	end
	wait(10)
	tn:remove()
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Seems like “cash” and “corpse” are significantly more likely than the last few since they have multiple entries. still should work, though you might have to do more tests to see ammo show up

1 Like

Try printing something in the output to make sure they atleast get fired

1 Like

Try doing wait(5) on all of them

1 Like

so only corpse is printing in the output :thinking: still cash and corpse drops only

still having the same problema

Try printing the chosen value right after “local chosen = dropitems[math.random(1,#dropitems)]”
this should atleast let you know if it actually picks other things from the list aswell

Is everything that could spawn anchored?

edit: I checked and it seems to work fine picking a random value but I also mostly get corpse and cash as these are the most common in the list, temporarily change the chosen value to what you’d like to test to see if the spawning actually works so you don’t have to keep hoping for the math.random to get it, once you see it works you can use math.random again

1 Like

this works fine you can check it with a for loop like below

for i = 1, 10 do
	local chosen = dropitems[math.random(1,#dropitems)] 
	print(chosen)
end

cbfc4ff472f814d4cdbf31d5be27d216

Also you should probably put the random drop line inside where the zombie died function

script.Parent.Died:connect(function()
	local chosen = dropitems[math.random(1,#dropitems)]  -- this makes the choice when the zombie dies
1 Like

ok i changed it a bit and i got these errors

local tn = script.Parent.Parent;
local dropitems = 
	{"Cash10","MediumAmmo","LargeAmmo","cash","corpse","cash","corpse","corpse","smallammo","corpse"}





script.Parent.Died:connect(function()
	local chosen = dropitems[math.random(1,#dropitems)]
	
	if chosen == "cash" then
		local item = game.ReplicatedStorage.Cash10:Clone()
		item.Parent = game.Workspace
		item.Position = script.Parent.Parent.UpperTorso.Position
		print("Cash")
	
	elseif chosen == "corpse" then
		wait(4)
		local item = game.ReplicatedStorage.Corpse:Clone()
		item.Parent = game.Workspace
		item:SetPrimaryPartCFrame(script.Parent.Parent.UpperTorso.CFrame)
		print("Corpse")
	
	
	elseif chosen == "smallammo" then
		local item = game.ReplicatedStorage.smallammo:Clone()
		item.Parent = game.Workspace
		item.Position = script.Parent.Parent.UpperTorso.Position
		print("small")
	
		
	elseif chosen == "MediumAmmo" then
		local item = game.ReplicatedStorage.MediumAmmo:Clone()
		item.Parent = game.Workspace
		item:SetPrimaryPartCFrame(script.Parent.Parent.UpperTorso.CFrame)
		print("medium")
	
		
	elseif chosen == "LargeAmmo" then
		local item = game.ReplicatedStorage.LargeAmmo:Clone()
		item.Parent = game.Workspace
		item:SetPrimaryPartCFrame(script.Parent.Parent.UpperTorso.CFrame)
		print("Large")
	end
	
	
	wait(10)
	tn:remove()
end)

the first and last error are because the items models you are cloning don’t have primaryparts set in them

and the middle is because you cloning a model and trying to use item.Position on it

1 Like