Help with GUI opening script

I am trying to open a frame via a button

--- {0.383, 0},{0.057, 0}
local ShopButton = script.Parent
local ShopFrame = game.Players.LocalPlayer.PlayerGui.BloxBurgSettingRipoff.MainBackground

ShopButton.MouseButton1Click:Connect(function()
	if ShopFrame.Visible == false then
		ShopFrame:TweenPosition(UDim2.new(0.383, 0, 0.057, 0), "Out", "Linear", .5, true)
		ShopFrame.Visible = true
		ShopButton.Text = "Close"

	else
		ShopFrame:TweenPosition(UDim2.new(1, 0, 0.057, 0), "Out", "Linear", .5, true)
		wait(.5)
		ShopFrame.Visible = false
		ShopButton.Text = "Settings"
	end
end)

Any error keeps poping up.

This is it

15:51:01.501 BloxBurgSettingRipoff is not a valid member of PlayerGui “Players.Madden21fan.PlayerGui” - Client - LocalScript:3

1 Like

you need to let the script catch up with the loading of the character.

local ShopButton = script.Parent
local ShopFrame = game.Players.LocalPlayer.PlayerGui:WaitForChild("BloxBurgSettingRipoff").MainBackground

ShopButton.MouseButton1Click:Connect(function()
	if ShopFrame.Visible == false then
		ShopFrame:TweenPosition(UDim2.new(0.383, 0, 0.057, 0), "Out", "Linear", .5, true)
		ShopFrame.Visible = true
		ShopButton.Text = "Close"

	else
		ShopFrame:TweenPosition(UDim2.new(1, 0, 0.057, 0), "Out", "Linear", .5, true)
		wait(.5)
		ShopFrame.Visible = false
		ShopButton.Text = "Settings"
	end
end)

This usually happens cause the StarterGui is still attempting to replicate & clone across the PlayerGui

Just call WaitForChild() to ensure that it’ll fully load in:

--- {0.383, 0},{0.057, 0}
local ShopButton = script.Parent
local Player = game.Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Gui = PlayerGui:WaitForChild("BloxBurgSettingRipoff")
local ShopFrame = Gui:WaitForChild("MainBackground")

ShopButton.MouseButton1Click:Connect(function()
	if ShopFrame.Visible == false then
		ShopFrame:TweenPosition(UDim2.new(0.383, 0, 0.057, 0), "Out", "Linear", .5, true)
		ShopFrame.Visible = true
		ShopButton.Text = "Close"

	else
		ShopFrame:TweenPosition(UDim2.new(1, 0, 0.057, 0), "Out", "Linear", .5, true)
		wait(.5)
		ShopFrame.Visible = false
		ShopButton.Text = "Settings"
	end
end)

You both are right.

@Jackscarlett and @Afraid4Life

Sadly I can only give one solution.

Sorry @Jackscarlett

Or you may simply do instead of waiting for the children of the PlayerGui,

script.Parent.Parent.Parent.BloxBurgSettingRipoff.MainBackground

Use Parent as many as the number of the ancestors connected to the script. Choose the PlayerGui folder the last ancestor. Before the script is loaded their ancestors will have already been loaded. So you could do this.