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.

function winnerGui()
    local plr = game.Players.LocalPlayer
    local plrGui = plr:WaitForChild("PlayerGui")
    local winnerGui = Instance.new("ScreenGui")
    winnerGui.Parent = plrGui
end

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.

function winnerGui()
    for _,plr in pairs(game.Players:GetPlayers())do
        local plrGui = plr:WaitForChild("PlayerGui")
        local winnerGui = Instance.new("ScreenGui")
        winnerGui.Parent = plrGui
    end
end
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