Cloning a tool and putting it in a players backpack

I am trying to clone a tool when a player spawns and place it in the Player’s backpack. This is the script I used.

game.Players.PlayerAdded:Connect(function(Player)
local clonetool = game.ServerStorage.TestTool:Clone()
clonetool.Parent = game.Players[Player.Name].Backpack
end)

For some reason, the tool won’t go into the Player’s backpack. If I set the parent of the tool to Server Storage then it works fine but in the backpack it doesn’t work.

Thanks for your help!

8 Likes

I managed to get it to work, however I had to add a substantial wait at the beginning in order to get it to work.

local Players = game:GetService(“Players”)
Players.PlayerAdded:Connect(function(Player)
Player:WaitForChild(“Backpack”)
wait(.1)
local clonetool = game.ServerStorage.TestTool:Clone()
clonetool.Parent = Players:FindFirstChild(Player.Name).Backpack
end)

4 Likes

You will most likely want to use the CharacterAdded event on Player. This guarantees the player’s character will be available, and will allow you to parent a tool to the player’s backpack. Also, if the player resets, it’ll give them the tool again.

17 Likes

I still couldn’t get it to work with the wait. Maybe I didn’t wait long enough but I did for like 30 seconds. Thanks for helping!

Try using the code snippet I provided and seeing if it works. I managed to get it to work on my end, so if it doesn’t work it may be another problem.

Add prints, is the code even running? Sometimes playeradded is connected AFTER your player has already been connected to the server. Also if there is a “Player” parameter being passed in you don’t have to do Players:FindFirstChild(Player.Name).Backpack. Just do Player:WaitForChild(“Backpack”).

1 Like

Sorry, I didn’t mean to reply to you, kinkocat.

Like others have mentioned, you should clone the tool after the CharacterAdded event is fired. Your original code wouldn’t work because the player’s Backpack is destroyed and then created every time the player’s character is created.

I realize that this has been solved, but I rather post this than have it go to waste. Here’s a quick example that I whipped up:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local backpack = player:WaitForChild("Backpack")
		local cloneTool = game.ServerStorage.TestTool:Clone()
		cloneTool.Parent = backpack
	end)
end)
16 Likes