Help with NPC dropping loot

How can i make this script drop more than 1 tool when i kill an NPC? everything works but i kind of need it to drop more than 1 item.

-- variables (Made By Er_1x / ERIC#2073)
local hum = script.Parent -- humanoid
local tfolder = game:GetService("ReplicatedStorage"):FindFirstChild("Items") -- finds the folder
local folderc = tfolder:GetChildren() -- gets the items on the folder

local IsModel = false -- check for model if its not a model leave it to false else set it to true
local IsRandom = true -- if set to false add a tool name to get the specific iten you want
local IsTool = true -- checks if its a tool or not IF IT IS A TOOL set it to true if not then flase
local ItemName = "" -- if israndom is set to true leave it blank

-- script part
hum.Died:Connect(function() -- function
	if IsRandom == true then
		if IsTool == false then
			local random = math.random(1, #folderc) -- randomizes
			local index = folderc[random] -- index the tool

			local item = index:Clone() -- clone the randomized tool
			item.Parent = game:GetService("Workspace") -- parent of the tool

			if IsModel == true then -- if it is a model or not dont change, only change the one on the variables
				item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
			else
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end

			print("Random Item Dropped: "..index.Name) -- notify
		else
			local random = math.random(1, #folderc) -- randomizes
			local index = folderc[random] -- index the item

			local tool = index:Clone() -- clone the randomized item
			tool.Parent = game:GetService("Workspace") -- parent of the item
			tool.Handle.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the item)

			print("Random Tool Dropped: "..index.Name) -- notify
		end
	else -- if random is not in true
		if IsTool == false then
			local item = tfolder:FindFirstChild(ItemName):Clone() -- gets the tool ( finds the tool inside the folder
			item.Parent = game:GetService("Workspace") -- parent of the tool

			if IsModel == true then -- if it is a model or not dont change, only change the one on the variables
				item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
			else
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end

			print("Item Dropped: "..ItemName) -- notify
		else
			local tool = tfolder:FindFirstChild(ItemName):Clone() -- gets the tool ( finds the tool inside the folder
			tool.Parent = game:GetService("Workspace") -- parent of the tool
			tool.Handle.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)

			print("Tool Dropped: "..ItemName) -- notify
		end
	end
end) -- end of function
1 Like

You should put the code that drops loot in a function like dropLoot. Then you can call it later like this:

local function dropTool()
   -- Code for dropping a tool
end

local function dropLoot(minDrops, maxDrops)
   for _ = 1, math.random(minDrops, maxDrops)
      dropTool()
   end
end

hum.Died:Connect(dropLoot)

You’re only ever making the NPC drop one tool, if you need it to drop multiple (randomly or always) then you need to add additional tools for it to drop.

-- variables (Made By Er_1x / ERIC#2073)
local hum = script.Parent -- humanoid
local tfolder = game:GetService("ReplicatedStorage"):FindFirstChild("Items") -- finds the folder
local folderc = tfolder:GetChildren() -- gets the items on the folder

local IsModel = false -- check for model if its not a model leave it to false else set it to true
local IsRandom = true -- if set to false add a tool name to get the specific iten you want
local IsTool = true -- checks if its a tool or not IF IT IS A TOOL set it to true if not then flase
local ItemName = "" -- if israndom is set to true leave it blank

-- script part
hum.Died:Connect(function() -- function
	local randomRepeat = math.random(1, 9)
	for i = 1, randomRepeat do
		if IsRandom then
			if IsTool == false then
				local random = math.random(1, #folderc) -- randomizes
				local index = folderc[random] -- index the tool

				local item = index:Clone() -- clone the randomized tool
				item.Parent = game:GetService("Workspace") -- parent of the tool

				if IsModel == true then -- if it is a model or not dont change, only change the one on the variables
					item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
				else
					item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
				end

				print("Random Item Dropped: "..index.Name) -- notify
			else
				local random = math.random(1, #folderc) -- randomizes
				local index = folderc[random] -- index the item

				local tool = index:Clone() -- clone the randomized item
				tool.Parent = game:GetService("Workspace") -- parent of the item
				tool.Handle.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the item)

				print("Random Tool Dropped: "..index.Name) -- notify
			end
		else -- if random is not in true
			if IsTool == false then
				local item = tfolder:FindFirstChild(ItemName):Clone() -- gets the tool ( finds the tool inside the folder
				item.Parent = game:GetService("Workspace") -- parent of the tool

				if IsModel == true then -- if it is a model or not dont change, only change the one on the variables
					item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
				else
					item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
				end

				print("Item Dropped: "..ItemName) -- notify
			else
				local tool = tfolder:FindFirstChild(ItemName):Clone() -- gets the tool ( finds the tool inside the folder
				tool.Parent = game:GetService("Workspace") -- parent of the tool
				tool.Handle.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)

				print("Tool Dropped: "..ItemName) -- notify
			end
		end
	end
end) -- end of function
local randomRepeat = math.random(1, 9)
	for i = 1, randomRepeat do

These were the 2 lines I added (3rd if you count the end statement at the end), this isn’t how you should do it but this is how you could do it, it’ll essentially execute the callback function connected to the .Died event a random number of times thus causing the NPC to seemingly drop a random number of times.

1 Like