Help with cloning parts to backpack

Hey everybody! So I’ve been working on a looting system. A very simple looting system. Everything is working fine except the cloning part. the NPC drops the part but whenever i walk over it, it doesn’t get cloned to my backpack. I have a folder in ServerStorage with all the models in it as well as a folder with the actual tools. Any help would be greatly appreciated.

-- 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

I don’t see the part of this script where the tool goes into the players backpack

1 Like

First of all, you may not want to use the GetService constructor function on workspace, just use game.Workspace or workspace. Second, using

is setting the tools parent to workspace, not checking if the tool is in the workspace.

2 Likes

Sorry about that. i had another script in which i was trying to achieve it. sorry if the script is very bad. i just started a few weeks ago and have been learning slowly.

local tfolder = game:GetService("ServerStorage"):WaitForChild("Pickups")

local folderc = tfolder:GetChildren() 
function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        part:Clone().Parent = player.Backpack
    end
end

Ah i see. Could that be an issue with cloning to a players backpack? I will make the changes you stated.

This would be the same as tool.Parent = game.Workspace or tool.Parent = workspace not sure why you would want to do it this way?

Where are you using/calling the function onTouched?
As long as the function dosent gets called, the backpack won‘t get cloned.

So, trying to use your own code, it should look something like this (if you don‘t understand something: just tell me)

game.Players.PlayerAdded(player):Connect(function() --for each player that‘s added to the game

    local HRP = script:FindFirstChild("HumanoidRootPart") --The humanoid root part (read the last comment for more)
    local tfolder = game:GetService("ServerStorage"):WaitForChild("Pickups")

    local folderc = tfolder:GetChildren() 
    function onTouched(hit)
        if player then
            part:Clone().Parent = player.Backpack
        end
    end

    HRP.Touched:Connect(onTouched) 
-- HRP is the HumanoidRootPart, basically the „whole“ model. Instead of adding the event to every separated part of the character, we can just use this one part.
end)

Okay so the issue here is because “Tool” instances don’t have access to the Touched event, Tool instances similar to ScreenGui/BBGui/SurfaceGui instances are more like container instances (for them to have any purpose they muse contain other instances), so in this case you will want the part named “Handle” of the tool instance which is a BasePart instance and does have access to the Touched event be the part which when touched clones the entire Tool instance to the player’s backpack.

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

Here’s a sample script to accompany the previous reply, when the “Handle” part of the tool (the instance which the script is parented to) is touched, the touching player is determine, the tool is then cloned into their backpack and after 1 second the tool is destroy, we ensure that this only happens when the tool is parented to the workspace by checking if the tool is parented to the workspace and not to a player’s character model or a player’s backpack folder.