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)
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)