Script cannot find ScreenGui

Hello Developers!
Today i was trying to make a script that moves guis after they click play button. There is an error always appearing in output and it says “CarSpawner is not valid member on playerGui” but there is a CarSpawner gui in playerGui, i would like what is wrong (I am totaly new in scripitng)
Here is the script:

local PlayButton = script.Parent.MainMenuBackground.HoverPlay
local background = script.Parent.Parent.CarSpawner:WaitForChild("BackgroundFrame")
local SpawnButton = script.Parent.Parent.CarSpawner:WaitForChild("SpawnCarButton")
local RemoveButton = script.Parent.Parent.CarSpawner:WaitForChild("RemoveCarButton")

local Opened = false

PlayButton.MouseButton1Click:Connect(function()
	if not Opened then
		Opened = true
		wait(1.5)
		background:TweenPosition(UDim2.new(0.011, 0,0.741, 0),"InOut","Quint", 0.5, true)
		SpawnButton:TweenPosition(UDim2.new(0.017, 0,0.756,0),"InOut","Quint", 0.5, true)
		RemoveButton:TweenPosition(UDim2.new(0.016, 0,0.872,0),"InOut","Quint", 0.5, true)
		for i = 0,10,1 do
			wait()
		end
	end
end)

Have a great day!

May you send an Explorer screenshot? If so maybe I can help you more.

car not workin
Here is the screenshot. @Imaginary_Syntax

I don’t see why you need to exit out of the GUI by doing

script.Parent.Parent.CarSpawner

You can just do:

script.Parent:FindFirstChild(object here)
local PlayButton = script.Parent.MainMenuBackground.HoverPlay
local background = script.Parent:WaitForChild("BackgroundFrame")
local SpawnButton = script.Parent:WaitForChild("SpawnCarButton")
local RemoveButton = script.Parent:WaitForChild("RemoveCarButton")

local Opened = false

PlayButton.MouseButton1Click:Connect(function()
	if not Opened then
		Opened = true
		wait(1.5)
		background:TweenPosition(UDim2.new(0.011, 0,0.741, 0),"InOut","Quint", 0.5, true)
		SpawnButton:TweenPosition(UDim2.new(0.017, 0,0.756,0),"InOut","Quint", 0.5, true)
		RemoveButton:TweenPosition(UDim2.new(0.016, 0,0.872,0),"InOut","Quint", 0.5, true)
		for i = 0,10,1 do
			wait()
		end
	end
end)

Try this ^

Hi. So “WaitForChild” can get children from other ScreenGuis without calling that screengui first?

Your script open/close is inside the CarSpawner GUI therefore there’s no need to go outside or the GUI to get objects which are inside the GUI. I’d suggest using the code above and report back if it works. :slight_smile:

that script is called “RemoveAllGuis” in “MainMenuGui” and not “Open/Close” in “CarSpawner” Gui.

Apologies, for that. Maybe try:

game.Players.LocalPlayer.PlayerGui.CarSpawner

I tried that before but same error occures.

Maybe add in a :WaitForChild(‘CarSpawner’), it may be that your calling the event and the gui may not have loaded.

OK, thanks for taking your time to help me :slight_smile:

May I ask what the purpose of this is?

Capitalization matters so try PlayerGui instead of playerGui with a WaitForChild()

Dont use wait() I have seen many people saying that it is bad to use wait() instead use game:GetService("RunService").HeartBeat:Wait()

if CarSpawner is in StarterGui then this should fix your problem.

local PlayButton = script.Parent.MainMenuBackground.HoverPlay
local background = game:GetService("StarterGui").CarSpawner.BackgroundFrame
local SpawnButton = game:GetService("StarterGui").CarSpawner.SpawnCarButton
local RemoveButton = game:GetService("StarterGui").CarSpawner.RemoveCarButton

local Opened = false

PlayButton.MouseButton1Click:Connect(function()
	if not Opened then
		Opened = true
		wait(1.5)
		background:TweenPosition(UDim2.new(0.011, 0,0.741, 0),"InOut","Quint", 0.5, true)
		SpawnButton:TweenPosition(UDim2.new(0.017, 0,0.756,0),"InOut","Quint", 0.5, true)
		RemoveButton:TweenPosition(UDim2.new(0.016, 0,0.872,0),"InOut","Quint", 0.5, true)
		for i = 0,10,1 do
			wait()
		end
	end
end)