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

Backpack is not a valid member of Player?

Asked by 9 years ago

Line 4 is throwing the error Backpack is not a valid member of Player. I can't see anything wrong with it. Can anybody help me?

while true do
    players = game.Players:GetChildren()
    for p = 1, #players do
        if players[p].Backpack.Spawned.Value == true then
            if players[p].PlayerGui.AmbienceDay.IsPlaying == false then players[p].PlayerGui.AmbienceDay:Play() end
            if players[p].PlayerGui.AmbienceNight.IsPlaying == false then players[p].PlayerGui.AmbienceNight:Play() end
            mult = 1
            if players[p].PlayerGui.InsideCheck.Inside.Value == true then mult = 0.1 end
            players[p].PlayerGui.AmbienceDay.Volume = game.Lighting.Weather.DayVolume.Value * mult
            players[p].PlayerGui.AmbienceNight.Volume = game.Lighting.Weather.NightVolume.Value * mult
        end
    end
    wait(0.5)
end
0
Try using Check-Statement after line 3 to see if the backpack is there, you may be calling on the backpack before it even loads. Muoshuu 580 — 9y
0
How would I do that? Ryan5124KBCP 15 — 9y
0
use game.Players:GetPlayers() 0xDEADC0DE 310 — 9y
1
^ That would do the exact same thing. @Poster if players[p]:FindFirstChild("Backpack") then CODE_HERE end Muoshuu 580 — 9y
0
add this "repeat wait until players[p]:FindFirstChild("Backpack")" RM0d 305 — 9y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago
while true do
    players = game.Players:GetPlayers() -- GetPlayers only gets Player objects. This is important if a non-Player object gets placed in Players for some reason.
    for _, plr in ipairs(players) do -- This is a generic for. The only change to the below code is that `players[p]` becomes simply `plr`:
        if plr:FindFirstChild("Backpack") and plr.Backpack.Spawned.Value then -- This addition should  fix your error. It won't run the rest of the code (including the second half of the `if` statement) unless the Backpack exists.
            if not plr.PlayerGui.AmbienceDay.IsPlaying then
                plr.PlayerGui.AmbienceDay:Play()
            end
            if not plr.PlayerGui.AmbienceNight.IsPlaying then
                plr.PlayerGui.AmbienceNight:Play()
            end
            mult = 1
            if r.PlayerGui.InsideCheck.Inside.Value then mult = 0.1 end
            plr.PlayerGui.AmbienceDay.Volume = game.Lighting.Weather.DayVolume.Value * mult
            plr.PlayerGui.AmbienceNight.Volume = game.Lighting.Weather.NightVolume.Value * mult
        --In the above `if` statements, I removed the `== true` and `== false`. `== true` is never explicitly necessary, and `== false` becomes `== true` when you use the `not` keyword on the expression.
        end
    end
    wait(0.5)
end
Ad

Answer this question