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.
1 | function winnerGui() |
2 | local plr = game.Players.LocalPlayer |
3 | local plrGui = plr:WaitForChild( "PlayerGui" ) |
4 | local winnerGui = Instance.new( "ScreenGui" ) |
5 | winnerGui.Parent = plrGui |
6 | end |
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.
1 | function 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 |
7 | end |