Cloning tool to backpack when joining problem

I trying to run a player join command that a tool will be put into their inventory. I am not using the StarterPack because I am trying to use DataStores. Can you help me with my code? There is no error in output when I run a server.

game.Players.PlayerAdded:Connect(function(player)
	
	local tool = game.ReplicatedStorage.Equippables["Blue Brick"]:Clone()
	tool.Parent = player.Backpack
	
end)

The equippables is the name of the folder.

Try and do player:WaitForChild(“Backpack”) instead.
Also maybe to startergear instead of backpack

1 Like

What are you using DataStores for exactly?

I think it’s StarterGear not StarterPack.

I’m making a DataStore that will save the player’s tool they had before leaving

This isn’t working as the character isn’t loaded as soon as the player connects. You need to do something similar to:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local tool = game.ServerStorage.Tool:Clone()
		tool.Parent = player.Backpack
	end)
end)

and then it should work.

2 Likes
local DataService = game:GetService("DataStoreService")
local ToolsData = DataService:GetDataStore("ToolsData")

local Equipables = game.ReplicatedStorage.Equippables

game.Players.PlayerAdded:Connect(function(plr)
	local toolsSaved = ToolsData:GetAsync(plr.UserId) or {}
	for i, toolSaved in pairs(toolsSaved) do
		if Equipables:FindFirstChild(toolSaved) then 
			Equipables[toolSaved]:Clone().Parent = plr:WaitForChild("Backpack")
		end
	end
	plr.CharacterRemoving:Connect(function(char)
		char.Humanoid:UnequipTools()
    end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsOwned = {}
	
	for i, Tool in pairs(plr.StarterGear:GetChildren()) do
		table.insert(toolsOwned, Tool.Name)
	end
	local success, errormsg = pcall(function()
		ToolsData:SetAsync(plr.UserId, toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)

You can use this script to save the players backpack before leaving and when they join back, they will recieve the tool again.

1 Like

I don’t need this. Thanks anyway.

You may be running into a problem that can happen where you’ve added to a Backpack that immediately
gets replaced:

So you may have to detect that the Backpack is replaced, or delay the parenting of the Tool to the
Backpack, say until CharacterAdded.