Can't make GUI visible using command

What I’m currently trying to do is to make the command “!notes” open 2 GUI’s to the localplayer only, not server GUI.

The issue is that I keep receiving this error when I use the command.

ServerScriptService.commands:146: attempt to index nil with 'PlayerGui'

I tried looking to see if I made a typo because “nil” would technically mean nothing and it doesn’t exist. Everything seems correctly spelt so I don’t know what mistake I am making here.

My code

game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(groupid) == 11 then
		player.Chatted:Connect(function(message)
			game.Players.LocalPlayer.PlayerGui.Notes.TextBox.Visible = true
			game.Players.LocalPlayer.PlayerGui.Notes.Save.Visible = true
		end)
	end
end)

Anybody know this error and how to fix it? Relatable?

You can’t access the local player from inside a serverscript. What you need to do is replace game.Players.LocalPlayer with player (As that’s the variable you create during the PlayerAdded event)

1 Like

Ohh, never knew about that. At least I learned something new from this. Thanks! It worked.

1 Like

(Also adding onto Post #2)

I’d recommend creating variables that’ll easily define where you can get the Player’s Gui, by using the WaitForChild() function, cause there’s the possibility where it might suddenly error with attempt to index nil with PlayerGui:

game.Players.PlayerAdded:Connect(function(player)
    local PlayerGui = player:WaitForChild("PlayerGui")
    local NoteFrame = PlayerGui:WaitForChild("Notes")

	if player:GetRankInGroup(groupid) == 11 then
		player.Chatted:Connect(function(message)
			NoteFrame.TextBox.Visible = true
			NoteFrame.Save.Visible = true
		end)
	end
end)
1 Like

I see, variables do make it easier and can also make my script neater. Thanks!

1 Like

It would be an entirely different error, PlayerGui is not a valid member of Player, but I don’t think that would ever happen with the PlayerGui.

2 Likes