[SOLVED] "MainGui is not a valid member of PlayerGui "Players.kdopdaux1803.PlayerGui""

Hello welcome to another issue.

This time it’s different.

I was helping @nosensehemor (@NonsenseYt2) and then the issue happened…

If you read the title, this is what is says.

Video:

LocalScript:

script.Parent["Menu (Frame)"].Play.MouseButton1Click:Connect(function()
	script.Parent.Enabled = false
end)

script.Parent["Menu (Frame)"].Quit.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer:Kick("Rejoin the game. If you do not want to play the game for a bit, join later !")
end)

if script.Parent.Enabled == true then
	game.Players.LocalPlayer.PlayerGui.MainGui.Enabled = false
	game.Players.LocalPlayer.PlayerGui.Warning.Enabled = false
	game.Players.LocalPlayer.PlayerGui.RandomName.Enabled = false
elseif script.Parent.Enabled == false then
	game.Players.LocalPlayer.PlayerGui.MainGui.Enabled = true
	game.Players.LocalPlayer.PlayerGui.Warning.Enabled = true
	game.Players.LocalPlayer.PlayerGui.RandomName.Enabled = true
end

So yeah guys any help is appreciated…

can i see the player gui in explorer, cause the problem seems to be that the MainFrame doesn’t exist…

This is what it looks like:

bandicam 2022-09-02 20-48-05-185

Please don’t force me to put those 4 guis in the Explorer.

where is the local script located?

bandicam 2022-09-02 20-51-32-450

honestly maybe just change the path to script.Parent.Parent.MainGui see if that works

Nope, it didn’t work unfortunately…

I think the issue when it was the same as i got a month ago that when the VersionGui was not a correct member of the PlayerGui.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

repeat task.wait() until LocalPlayer:FindFirstChild("PlayerGui")
local PlayerGui = LocalPlayer.PlayerGui

local Gui = script.Parent

Gui["Menu (Frame)"].Play.MouseButton1Click:Connect(function()
Gui.Enabled = false
end)

Gui["Menu (Frame)"].Quit.MouseButton1Click:Connect(function()
LocalPlayer:Kick("Rejoin the game. If you do not want to play the game for a bit, join later !")
end)

if script.Parent.Enabled then
PlayerGui.MainGui.Enabled = false
PlayerGui.Warning.Enabled = false
PlayerGui.RandomName.Enabled = false

else

PlayerGui.MainGui.Enabled = true
PlayerGui.Warning.Enabled = true
PlayerGui.RandomName.Enabled = true
end

Sorry for indentation.

1 Like

Thank you.

But i’ve seen to another issue.

No problem, have a nice day! :slightly_smiling_face:

@paetemc2 Thanks for trying to help me.
@spvcta Thanks for the help !

Most likely the local script is being run before the guis are fully added to the PlayerGui. I would just add :WaitForChild() calls for the guis you are trying to index.

I would NOT recommend using the solution above, as repeat task.wait() is usually one of the strategies devs try to avoid when coding because it can be inefficient and inconsistent. Also, in this case the use of it doesn’t even make sense since PlayerGui always exist under players. Not sure how that even fixed the issue tbh

This is the code that add the :WaitForChild() calls.

script.Parent["Menu (Frame)"].Play.MouseButton1Click:Connect(function()
	script.Parent.Enabled = false
end)

script.Parent["Menu (Frame)"].Quit.MouseButton1Click:Connect(function()
	game.Players.LocalPlayer:Kick("Rejoin the game. If you do not want to play the game for a bit, join later !")
end)

if script.Parent.Enabled == true then
	game.Players.LocalPlayer.PlayerGui:WaitForChild("MainGui").Enabled = false
	game.Players.LocalPlayer.PlayerGui:WaitForChild("Warning").Enabled = false
	game.Players.LocalPlayer.PlayerGui:WaitForChild("RandomName").Enabled = false
elseif script.Parent.Enabled == false then
	game.Players.LocalPlayer.PlayerGui:WaitForChild("MainGui").Enabled = true
	game.Players.LocalPlayer.PlayerGui:WaitForChild("Warning").Enabled = true
	game.Players.LocalPlayer.PlayerGui:WaitForChild("RandomName").Enabled = true
end

Are you sure about this? This is solved.

I usually don’t post on solved items unless I think the solution is flawed. So, am I sure? To the best of my knowledge. I’ve been programming in Roblox for a long time. If my solution works, I would recommend using it instead.

It’s also working, but i think, i might prefer the script that @spvcta posted.