Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)
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. mikey2019d 43 — 4y
0
Print the number of instances in the backpack as well and see what it outputs. Loughdough 291 — 4y
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 mikey2019d 43 — 4y
0
I would put the code inside the .CharacterAdded event because tools get removed when you die. COUNTYL1MITS 312 — 4y
0
You could just put the tools in starter pack deadwalker601 110 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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

Answer this question