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

Backpack is not a vaild member of player??

Asked by
Jirozu 71
7 years ago

The script doesnt find the player's backpack in enough time to give the player their power!, here is the script:

local Players = game:GetService("Players")


function onPlayerAdded(player)

local Quirks = game.ServerStorage.Quirks:GetChildren()
local RandomizeQuirks = math.random(1, #Quirks)
local QuirksChosen = Quirks[RandomizeQuirks]
print(QuirksChosen)
local quirk = game.ServerStorage.Quirks:FindFirstChild(QuirksChosen.Name):clone()
repeat wait() until Players.Backpack or Players.Backpack.Parent

quirk.Parent = Players.BackPack
end

Players.PlayerAdded:connect(onPlayerAdded)

for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end

Any help?

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Your parameter for the PlayerAdded function is 'player', and you're using the globally defined variable 'Players'.

Keep track of your variables.

Also, when you find the randomized quirk, you already have referenced the object. There's no point in searching for it again on line 10.

Another thing, on line 13 you're indexing "BackPack". The 'P' should not be capitalized.

local Players = game:GetService("Players")
local Quirks = game.ServerStorage.Quirks
--^You use it multiple times, so make it a variable

function onPlayerAdded(player)
    local CurrentQuirks = Quirks:GetChildren()
    local QuirkChosen = Quirks[math.random(1, #CurrentQuirks)]
    print(QuirkChosen)
    local quirk = QuirkChosen:Clone()
    local backpack = player:WaitForChild("Backpack")
    quirk.Parent = backpack
end

Players.PlayerAdded:connect(onPlayerAdded)

for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end
Ad

Answer this question