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

Why is this saying my value is a 'nil' value?

Asked by 9 years ago

I am making a minigame type game and when a round ends, a Gui goes on everyone's screens that tells them who won. Well, my core game loop isn't working because my Gui function keeps erroring.

Here is my error message.

20:39:10.170 - Workspace.Editing:110: attempt to index local 'plr' (a nil value)

Why am I getting this? I can't find a workaround or solution for it.

1function winnerGui()
2    local plr = game.Players.LocalPlayer
3    local plrGui = plr:WaitForChild("PlayerGui")
4    local winnerGui = Instance.new("ScreenGui")
5    winnerGui.Parent = plrGui
6end

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

LocalPlayer is only defined in a LocalScript that is running on the client. It is used to get the client's player, because they're the one running the script. If the script is on the server, nobody is running it locally, so there is no local player.

Instead, what you want to do is iterate through every player.

1function winnerGui()
2    for _,plr in pairs(game.Players:GetPlayers())do
3        local plrGui = plr:WaitForChild("PlayerGui")
4        local winnerGui = Instance.new("ScreenGui")
5        winnerGui.Parent = plrGui
6    end
7end
0
Thank you! I wasn't aware of that. megamario640 50 — 9y
0
what's up with the thumb down someone gave here? 1waffle1 2908 — 9y
Ad

Answer this question