Ad
Log in to vote
0

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

Asked by 1 year ago

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!
0
LocalPlayer can only be called from a LocalScript (a client). Based on your error message, you are calling it from the server. appxritixn 2235 — 1y

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

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

Ad

Answer this question