Help with looting system

I have 2 folders in “ServerStorage”. one folder has the functional tools and the other has the visual pickups (models). everything works fine except one thing. I cant seem to figure out how to clone the visual pickups (models) into the players backpack. so in shorter terms i need to be able to pick up the model which then gets cloned as it’s respective tool to the players backpack. heres the script. (it doesn’t clone the model to the players back, im not sure how to do that)

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

local IsModel = true -- 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 = false -- 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, 2)
	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

You don’t actually parent the tools to the players backpack, you set the parent to the Workspace. For tools to appear in the players backpack you need to either get the backpack and set that as the parent of the tool or you could also use Player:EquipTool() if you want the player to equip the tool immediately, this puts the tool in the backpack and equips it.

From developer hub documentation

local function giveTool(player, tool)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		tool.Parent = backpack
	end
end

local Players = game:GetService("Players")
 
local player = Players:FindFirstChildOfClass(“Player”)
if player and player.Character then
	local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local tool = workspace:FindFirstChild("Tool")
		if tool then
			humanoid:EquipTool(tool)	
		end
	end
end

I think it’s supposed to clone the item and spawn it into workspace.
Yk like an item spawning system

My understanding of the OP is that they want the tool to be added to the players backpack, perhaps I misunderstood.

1 Like

I’d assume one of the two cases above as well, but you two talking about whether or not it’s what they wanted made me notice.
There’s quite a lot of usage of the term “Model”, and the following portion of code:

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

Makes me wonder, if the above solutions are not what he wanted. I’d assume he wants to clone a Model at the Player’s Death Location, as he did call them “visual pickups (models)”. Which from what I’m guessing is supposed to give tools as well.

If this is the case this is what I would do:

  1. Keep the Models Consistent, all with a “Hitbox”, best not to rotate it. Create joints or welds too for when you move it.
  2. Create a Ray downwards of the player to get the location of the Floor.
  3. Move the Hitbox to the SurfaceLocation, offset by the Hitbox’s YSize/2 to align the bottom of the hitbox above the floor. If no surface is found stick it the RootPart to stay safe.
  4. Add a listener to the Hitbox for when a player touches, to provide the gear and destroy itself.
local handle = script.Parent
local tool = handle.Parent
local debounce = false

if tool.Parent.Name == "Workspace" then --is in workspace
	local connection = handle.Touched:Connect(function(hit)
		if debounce then
			return
		end
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			debounce = true
			tool:Clone().Parent = player.Backpack
			task.wait(1)
			tool:Destroy()
		end
		task.wait(3)
		debounce = false
	end)
end

I responded to the other thread you posted but here’s the same example script again.

local IsModel = true -- 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 = false -- checks if its a tool or not IF IT IS A TOOL set it to true if not then flase

You’re never even changing any of these 3 values.

1 Like
-- variables (Made By Er_1x / ERIC#2073)
local hum = script.Parent -- humanoid
local tfolder = game:GetService("ServerStorage")
local pickups = tfolder:WaitForChild("Pickups") -- finds the folder
local folderc = pickups:GetChildren() -- gets the items on the folder

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

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

			if index:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif index:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end

			local item = index:Clone() -- clone the randomized tool
			item.Parent = workspace -- parent of the tool

			if IsModel 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
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
			
			print("Random Item/Tool Dropped: "..index.Name) -- notify	
		else -- if random is not in true
			local selection = tfolder:FindFirstChild(ItemName)
			
			if selection:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif selection:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end
			
			local item = selection:Clone() -- gets the tool ( finds the tool inside the folder
			item.Parent = workspace -- parent of the tool
			
			if IsModel 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
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
		end
	end
end) -- end of function

So this now checks if the thing being cloned is a model/tool and sets the boolean variables accordingly.

-- variables (Made By Er_1x / ERIC#2073)
local players = game:GetService("Players")
local hum = script.Parent -- humanoid
local tfolder = game:GetService("ServerStorage")
local pickups = tfolder:WaitForChild("Pickups") -- finds the folder
local folderc = pickups:GetChildren() -- gets the items on the folder

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

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

			if index:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif index:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end

			local item = index:Clone() -- clone the randomized tool
			--item.Parent = workspace -- parent of the tool

			if IsModel then -- if it is a model or not dont change, only change the one on the variables
				local distances = {}
				for _, plr in ipairs(players:GetPlayers()) do
					distances[plr.Name] = plr:DistanceFromCharacter(hum.Parent.Head.Position)
				end
				if #distances >= 1 then
					table.sort(distances, function(a, b)
						return a < b
					end)
				end
				for plrName, distance in pairs(distances) do
					local plr = players:FindFirstChild(plrName)
					if plr then
						local bp = plr.Backpack
						item.Parent = bp
					end
				end
				--item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
			
			print("Random Item/Tool Dropped: "..index.Name) -- notify	
		else -- if random is not in true
			local selection = tfolder:FindFirstChild(ItemName)
			
			if selection:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif selection:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end
			
			local item = selection:Clone() -- gets the tool ( finds the tool inside the folder
			--item.Parent = workspace -- parent of the tool
			
			if IsModel then -- if it is a model or not dont change, only change the one on the variables
				local distances = {}
				for _, plr in ipairs(players:GetPlayers()) do
					distances[plr.Name] = plr:DistanceFromCharacter(hum.Parent.Head.Position)
				end
				if #distances >= 1 then
					table.sort(distances, function(a, b)
						return a < b
					end)
				end
				for plrName, distance in pairs(distances) do
					local plr = players:FindFirstChild(plrName)
					if plr then
						local bp = plr.Backpack
						item.Parent = bp
					end
				end -- 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
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
		end
	end
end) -- end of function

and this will parent the dropped models to the backpack of the player who is closest to the NPC when they die and drop lootable equipment.

I never got any replies on my end on my other post. I thought i knew what i was doing in terms of changing values but apparently Im not that far ahead yet. I also tried the script you provided

-- variables (Made By Er_1x / ERIC#2073)
local players = game:GetService("Players")
local hum = script.Parent -- humanoid
local tfolder = game:GetService("ServerStorage")
local pickups = tfolder:WaitForChild("Pickups") -- finds the folder
local folderc = pickups:GetChildren() -- gets the items on the folder

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

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

			if index:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif index:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end

			local item = index:Clone() -- clone the randomized tool
			--item.Parent = workspace -- parent of the tool

			if IsModel then -- if it is a model or not dont change, only change the one on the variables
				local distances = {}
				for _, plr in ipairs(players:GetPlayers()) do
					distances[plr.Name] = plr:DistanceFromCharacter(hum.Parent.Head.Position)
				end
				if #distances >= 1 then
					table.sort(distances, function(a, b)
						return a < b
					end)
				end
				for plrName, distance in pairs(distances) do
					local plr = players:FindFirstChild(plrName)
					if plr then
						local bp = plr.Backpack
						item.Parent = bp
					end
				end
				--item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
			
			print("Random Item/Tool Dropped: "..index.Name) -- notify	
		else -- if random is not in true
			local selection = tfolder:FindFirstChild(ItemName)
			
			if selection:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif selection:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end
			
			local item = selection:Clone() -- gets the tool ( finds the tool inside the folder
			--item.Parent = workspace -- parent of the tool
			
			if IsModel then -- if it is a model or not dont change, only change the one on the variables
				local distances = {}
				for _, plr in ipairs(players:GetPlayers()) do
					distances[plr.Name] = plr:DistanceFromCharacter(hum.Parent.Head.Position)
				end
				if #distances >= 1 then
					table.sort(distances, function(a, b)
						return a < b
					end)
				end
				for plrName, distance in pairs(distances) do
					local plr = players:FindFirstChild(plrName)
					if plr then
						local bp = plr.Backpack
						item.Parent = bp
					end
				end -- 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
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
		end
	end
end) -- end of function

but now nothing is dropping from the NPC. Isn’t = nil just destroying the values and removing the tools from the game? Again, im very new to writing code so please excuse me if my logic is off.

-- variables (Made By Er_1x / ERIC#2073)
local players = game:GetService("Players")
local hum = script.Parent -- humanoid
local tfolder = game:GetService("ServerStorage")
local pickups = tfolder:WaitForChild("Pickups") -- finds the folder
local folderc = pickups:GetChildren() -- gets the items on the folder

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

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

			if index:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif index:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end

			local item = index:Clone() -- clone the randomized tool
			--item.Parent = workspace -- parent of the tool

			if IsModel then -- if it is a model or not dont change, only change the one on the variables
				local distances = {}
				for _, plr in ipairs(players:GetPlayers()) do
					distances[plr.Name] = plr:DistanceFromCharacter(hum.Parent.Head.Position)
				end
				if #distances >= 1 then
					local shortest = math.huge
					local nameOfPlr = nil
					for plrName, distance in pairs(distances) do
						if distance <= shortest then
							shortest = distance
							nameOfPlr = plrName
						end
					end
					if nameOfPlr then
						local plr = players:FindFirstChild(nameOfPlr)
						if plr then
							local bp = plr.Backpack
							item.Parent = bp
						end
					end -- if it is a model or not dont change, only change the one on the variables
				end
				--item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end

			print("Random Item/Tool Dropped: "..index.Name) -- notify	
		else -- if random is not in true
			local selection = tfolder:FindFirstChild(ItemName)

			if selection:IsA("Model") then
				IsModel = true
				IsTool = false
			elseif selection:IsA("Tool") then
				IsModel = false
				IsTool = true
			else
				print("Something went wrong!")
			end

			local item = selection:Clone() -- gets the tool ( finds the tool inside the folder
			--item.Parent = workspace -- parent of the tool

			if IsModel then -- if it is a model or not dont change, only change the one on the variables
				local distances = {}
				for _, plr in ipairs(players:GetPlayers()) do
					distances[plr.Name] = plr:DistanceFromCharacter(hum.Parent.Head.Position)
				end
				if #distances >= 1 then
					local shortest = math.huge
					local nameOfPlr = nil
					for plrName, distance in pairs(distances) do
						if distance <= shortest then
							shortest = distance
							nameOfPlr = plrName
						end
					end
					if nameOfPlr then
						local plr = players:FindFirstChild(nameOfPlr)
						if plr then
							local bp = plr.Backpack
							item.Parent = bp
						end
					end -- if it is a model or not dont change, only change the one on the variables
				end
				--item:MoveTo(hum.Parent.Head.Position) -- how we can move it because its a model
			elseif IsTool then
				item.CFrame = hum.Parent.Head.CFrame -- cframe ( position of the tool)
			end
		end
	end
end) -- end of function

Nothing is dropping from the npc with this code.

can you print the folderc table?

Isn’t that unnecessary? I have a model and a tool both named the same name. The model drops on npc death and i need to be able to pick it up and it send the tool with the corresponding name to my inventory.

what if the index is the name of the tool and not the index itself?, also where does the script reside, where is it placed? sorry for the late reply though.

Good point on the index part. And the script goes inside the humanoid. I tried creating a seperate script to clone the model to the backpack with no luck.

For the first part I would recommend adding a new loop to insert the previous table’s contents to the new one just break the old table since we won’t be needing that. For the next one, you can probably detect a player’s death by using a reference to their humanoid probably through local Character = workspace["SOME_PLAYER_NAME"] but I recommend just for looping the models of players and then accessing the humanoid from it. Then detect if it died. Another possible situation is where all their attachments are removed and that means the character is broken which means they’ve died in the game. Sorry for the long and late response though (again).