Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix attempt to index nil with 'PlayerGui'?

I've been trying to code some dialogue, it works perfectly so I decided to add options to it. But for some reason I keep getting this error that I don't usually encounter in coding. I'm sorta just a beginner so can anyone help me?

--Error message was:
Players.Dragonboy99711.PlayerGui.MainGui.DialogueFrame.OptionsFrame.Option1.Script:7: attempt to index nil with 'PlayerGui'  -  Server - Script:7

--Here's the code:
local createDialogueEvent = game.ReplicatedStorage.Remotes.createDialogueEvent
local hideDialogueEvent = game.ReplicatedStorage.Remotes.hideDialogueEvent
local setDialogueImageEvent = game.ReplicatedStorage.Remotes.setDialogueImageEvent
local SetoptionsEvent = game.ReplicatedStorage.Remotes.optionsEvent

local player = game.Players.LocalPlayer
local dialogueFrame = player.PlayerGui.MainGui.DialogueFrame
local optionsFrame = player.PlayerGui.MainGui.DialogueFrame.OptionsFrame

script.Parent.MouseButton1Click:Connect(function()
	if optionsFrame.Option3.Text == "I would like a 2 number 9s, number 6 with extra dips,..." then
		SetoptionsEvent:FireAllClients("","","","")
		createDialogueEvent:FireAllClients("no")
		wait(1.5)
		createDialogueEvent:FireAllClients("ok go away if your not here for what ur meant to do")
		wait(3)
		hideDialogueEvent:FireAllClients()
		wait(0.9)
		wait(1)
	end
end)

--Ignore the dialogue lol i was just writing this for a friend!

1 comment

0

LocalPlayer can only be called from a LocalScript (a client). Based on your error message, you are calling it from the server.

1 answer

I can't really test your game so I don't know the exact problem but I can tell you that when it says something (a) can't be indexed with something else (b) like var = a.b, means the first thing (a) is nil, which means it doesn't exist, for some reason, game.Players.LocalPlayer doesn't seem to exist there, I don't know why but you must figure it out. See if anything runs before the player is added and stuff. Actually I have a suggestion. Try changing it to playergui = player :WaitForChild"PlayerGui", it's safer so you should always use waitforchild, also, that could be the problem, try it.

Hope this helps :3

This discussion is preserved as it was. New answers, comments, and votes are closed.