How to fix problem with attempt to index nil with player gui?

Hi, devs, I will script a dialogue GUI with a module script today, but it’s saying “Attempt to index nil with player GUI” for no reason. There is a module script I’ve created:

local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui
local dialogGui = playerGui:WaitForChild("MessageGui")
local dialogFrame = dialogGui:WaitForChild("DialogFrame")

local dialog = {
	["Symbols"] = {
		["Critical"] = "rbxassetid://16226114494",
		["Warning"] = "rbxassetid://16226157779",
		["Question"] = "rbxassetid://16226142386",
		["Information"] = "rbxassetid://16226130114"
	},
	["ButtonsOptions"] = {
		[0] = dialogFrame.OkButton,
		[1] = dialogFrame.QuestionButtons,
		[2] = dialogFrame.BuyGamepassButton,
		[3] = dialogFrame.BuyPremiumButton
	}
}

function dialog.createDialog(title, symbol, message, option)
	if dialog then
		dialogFrame.Title = title
		dialogFrame.Symbol =  dialog.Symbols[symbol]
		dialogFrame.Message = message
		dialogFrame.Visible = true
		
		if option == 0 then
			dialogFrame.OkButton.Visible = true
			wait()
			return dialogFrame.OkButton.MouseButton1Click:Wait()
			
		elseif option == 1 then
			dialogFrame.QuestionButtons.Visible = true
			
			local buttonClicked = false
			
			local function clickEvent(button)
				button.MouseButton1Click:Connect(function()
					buttonClicked = true
					dialogFrame.QuestionButtons.Visible = false
				end)
				
				while not buttonClicked do wait() end
				
				return button.Name
			end
			
			return clickEvent(dialog.ButtonsOptions[option][1]), clickEvent(dialog.ButtonsOptions[option][1])
			
		elseif option == 2 then
			dialogFrame.BuyGamepassButton.Visible = true
			wait()
			return dialogFrame.BuyGamepassButton.MouseButton1Click:Wait()
			
		elseif option == 3 then
			dialogFrame.BuyPremiumButton.Visible = true
			wait()
			return dialogFrame.BuyPremiumButton.MouseButton1Click:Wait()
		end
		
	else return end
end

return dialog

How do I fix problem with attempt to index nil with player GUI?

Instead of indexing player’s gui directly trough ., try instead indexing trough WaitForchild()

LocalPlayer only works in LocalScripts. It doesn’t work anywhere else