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

Cant open another Screen Gui from local script?

I recently started to try things in lua and i came across something i cant seem to find out. I also tried searching it on google but since i dont really know how to formulate it correctly im having a bit of a hard time with it. (I'm sorry if the title isnt clear, i couldnt think of a good way to explain/ask the question.)

So, i got the following script as a example. As you see i made a variable for "startmenu" Now when i run the game i get the following output: StartMenu is not a valid member of PlayerGui. I dont really get the problem since StartMenu is a member of StarterGui? I'm trying to open the Screen Gui "StartMenu" from a localscript that is located inside the "SideMenu" folder, i thought it would be possible to open it this way or is it impossible and do i need to find another way?

startmenu = script.Parent.Parent.Parent.StartMenu
local button = script.Parent
local toggled = false
 
local function onButtonActivated()
	if toggled == false then
		startmenu.Visible = true
		print ("open") -- prints if succesfully opened
		toggled = true
	else		
		print ("closed") -- prints if succesfully closed
		toggled = false
	end
end

button.Activated:Connect(onButtonActivated)

My StarterGui is looking like this: (i made it a code block so the tabs work)

StarterGui
	LoadingScreen (Screen Gui)
	SideMenu (Screen Gui)
		ImageButton
			Local Script 
	StartMenu (Screen Gui)

Thanks in advance!

3 comments

0

Which line is the error?

0

If you're available, could you take a look at my proposition?

0

I revised the pathway code, try it again!

2 answers

✓ Accepted answer

This is likely due to the script running faster than the asset can load. It's also a rough practice to use a continuous .Parent structure reference, so it's easiest if you descended from the Main Instance of the hierarchy.

To actively tell the script to wait for the "missing" asset, is to use the method of Instance, WaitForChild. This will yield the code until the asset can be properly declared. To gain access to the main ancestor (main instance) is to also use the method FindFirstAncestor, and give it the start point of our script object.

local StarterGuiMain = script:FindFirstAncestor("PlayerGui")
local StartMenu = StarterGuiMain:WaitForChild("StartMenu")
--// Note: declaring a local variable in the main scope calls it global

2 comments

0

Thank you for the tip, its better arranged like this! But with the code above i get the following error when testing "Players.Zeroxonion.PlayerGui.SideMenu.ImageButton.LocalScript:2: attempt to index local 'StarterGuiMain' (a nil value)"

0

My bad! I made a rookie mistake

You should yield the script to wait for the child. Maybe the script ran so fast it loaded before the gui did. Thus the error.

local startmenu = script.Parent.Parent.Parent:WaitforChild('StartMenu')

1 comment

0

With the script you posted i got the following error: WaitforChild is not a valid member of PlayerGui

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