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

Backpack is not a valid member of Player? What did I do wrong?

here is the script.

game.Players.PlayerAdded:Connect(function(player)
    while true do
        if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 4940518) then
            if player.Backpack:FindFirstChild("Spinner") == nil and player.Character:FindFirstChild("Spinner") == nil then
                print(player.Name .. " got an item!")
                local b = game.Lighting:FindFirstChild("Spinner"):Clone()
                b.Parent = player.Backpack
            end
        end
end
end)

2 comments

0

userId should be UserId. Plus, you shouldn't store items in Lighting. You should store items in ReplicatedStorage or ServerStorage. You also need some type of delay in your while loop. If not, it will crash. So add a wait under while true do.

0

you need to wait for the backpack, it won't necessarily be there as soon as the player joins the game

1 answer

✓ Accepted answer

Hello, Zeppelin0330!

Try:

game.Players.PlayerAdded:Connect(function(player)
    while true do
        if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 4940518) then
            if player:WaitForChild("Backpack"):FindFirstChild("Spinner") == nil and player.Character:FindFirstChild("Spinner") == nil then
                print(player.Name .. " got an item!")
                local b = game.Lighting:FindFirstChild("Spinner"):Clone()
                b.Parent = player.Backpack
            end
        end
	wait()
end
end)

I just changed the .Bakcpack to :WaitForChild("Backpack") (I made it only on the first line bacause it only need to wait for it 1 time)

Also, always place a wait() on your while, it will break to don`t cause lag

And Roblox Created 'ReplicatedStorage' to save models you will use scripts to place on the game

Good Luck with your games

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