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

Backpack is not a valid member of Player?

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

5 comments

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.

0

How would I do that?

0

use game.Players:GetPlayers()

1

^ That would do the exact same thing. @Poster if players[p]:FindFirstChild("Backpack") then CODE_HERE end

0

add this "repeat wait until players[p]:FindFirstChild("Backpack")"

1 answer

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

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