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
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.
Print the number of instances in the backpack as well and see what it outputs.
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
I would put the code inside the .CharacterAdded event because tools get removed when you die.
You could just put the tools in starter pack