Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this script not cloning and setting the parent value to backpack?

So I am making a shop script and I ran into a problem when a player joins for the first time and has nothing in Backpack this script should run and adds started tools into their backpack. Instead, the script does not run.

Script:

game.Players.PlayerAdded:Connect(function(player)
	if #(player:FindFirstChild("Backpack"):GetChildren()) == 0 or nil then
		local x = game.ServerStorage.Loadout.Primary:Clone()
		x.Parent = player:FindFirstChild("Backpack")
		x.Name = "Primary"
		local y = game.ServerStorage.Loadout.Secondary["GLOCK 17"]:Clone()
		y.Parent = player:FindFirstChild("Backpack")
		y.Name = "Secondary"
	end
end)

5 comments

0

There is also no error in the output, and I also verified if it is running by printing true at the beginning which it does.

0

Print the number of instances in the backpack as well and see what it outputs.

0

Thanks but i found the problem to be that the function is firing the moment i join so I need to wait until back pack is existing in player

0

I would put the code inside the .CharacterAdded event because tools get removed when you die.

0

You could just put the tools in starter pack

1 answer

This should work

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        if #(player:FindFirstChild("Backpack"):GetChildren()) == 0 or nil then
            local x = game.ServerStorage.Loadout.Primary:Clone()
            x.Parent = player:FindFirstChild("Backpack")
            x.Name = "Primary"
            local y = game.ServerStorage.Loadout.Secondary["GLOCK 17"]:Clone()
            y.Parent = player:FindFirstChild("Backpack")
            y.Name = "Secondary"
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.