Attempt to index nil with 'MainFrame'

Module Script:

local Players = game:GetService("Players")

local module = {}

module.GetIntermissionGui = function(): GuiMain -- I'm not sure about "GuiMain"
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			local IntermissionGui = v.PlayerGui:WaitForChild("IntermissionGui")
			return IntermissionGui
		end
	end
end	
	
return module

Regular Script:

local ModuleScripts = SSS.ModuleScripts
local MainModule = require(ModuleScripts.MainModule)

local IntermissionGui = MainModule.GetIntermissionGui()

function StartIntermission()
	
	for i = 20, 0, -1 do

		IntermissionGui.MainFrame.MainLabel.Text = "Intermission: "..i -- Error here, with "attempt to index nil with 'MainFrame'"
		
		wait(1)

		if i == 0 then
			IntermissionGui.MainFrame.MainLabel.Text = ""
		end
		
	end
	
	if #Players:GetPlayers() > 1 then
		
		StartRoundEvent:Fire()
		print("Fired Start Round Event")
		
	else
		IntermissionGui.MainFrame.NotEnoughLabel.Text = "Atleast 2 players are needed"
		wait(4)
		IntermissionGui.MainFrame.NotEnoughLabel.Text = ""
		print("Not Enough Players")
		StartIntermissionEvent:Fire()
	end
end

StartIntermission()

I’m not sure how to fix this.

If you need more information I’ll gladly provide.
Any help is appreciated!
(i’ve also used WaitForChild() function, but that didn’t seem to work either)

It seems as though you are trying to locate the Player’s GUI component from a server-side context. Server scripts aren’t able to view the player’s GUI components as they are all generated clientside.

2 Likes

I’ve done it before in a for loop, but maybe what I’m trying to do here is impossible.

Is there another way I could do this? With remote events, etc?

I identified another error in your code, your GetIntermissionGui code will iterate through all the players but always return the intermission GUI of the first player in the table. I would suggest iterating through Players:GetChildren() inside your regular script and then passing the player object to your MainModule.GetIntermissionGui() function.

And as @AgencyDrone suggested, use remote events since modifying the UI from the server side of things is simply not possible. For your use case specifically, I would recommend creating a remote and then doing FireAllClients or FireClient depending on what you are looking for then running the UI manipulation code on the client side of things. That should work out for you.

1 Like

Yeah, remote events works perfectly, thank you!

1 Like