Issue with calling a button from another ScreenGUI?

So I have this button (in a screengui) that if you click it, it will open a frame (in another screengui, not the same one) but I feel lost and I keep getting these errors I don’t know how to fix.

Picture to explain:
Screenshot_3
The “CodesButton” inside the screengui “Main” is the button where if I click it, the “Frame” should appear, which is found inside “CodesGui”.

Here is the localscript inside “CodesGui”:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local CodesFunction = ReplicatedStorage.CodesFunction
local Frame = script.Parent.Frame
local Redeem = script.Parent.Frame.Redeem
local TextBox = script.Parent.Frame.TextBox
local IsOpen = false

local player = game.Players.LocalPlayer

local CurrentTween = nil

script.Parent.MouseButton1Click:Connect(function()
	
	if CurrentTween then
		CurrentTween:Canel()
		CurrentTween = nil
	end

	local Tween = TweenService:Create(Frame, TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {
		Position = (IsOpen and UDim2.new(0.5, 0, 1.5, 0) or UDim2.new(0.5, 0, 0.5, 0))
	})
	CurrentTween = Tween
	Tween:Play()

	IsOpen = not IsOpen
end)


Redeem.MouseButton1Click:Connect(function() -- On Mouse click on the redeem button
	local Result = CodesFunction:InvokeServer(TextBox.Text) -- Invokes the server, sends the code that the player has put in the TextBox

	if Result == 3 then
		TextBox.PlaceholderText = "Code successfully redeemed!"
	elseif Result == 2 then
		TextBox.PlaceholderText = "Code expired!"
	elseif Result == 1 then
		TextBox.PlaceholderText = "Code invalid/ already redeemed!"
	end

	TextBox.Text = ""
	wait(2)
	TextBox.PlaceholderText = "Enter code here..."
end)

It keeps giving me this error:
Screenshot_4

And here is the localscript inside “CodesButton”, if it helps:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	player.PlayerGui.CodesGui.Frame.Visible = not player.PlayerGui.CodesGui.Frame.Visible
end)

Please explain thoroughly as I feel like I don’t understand much of whats going on…

You might need to use “WaitForChild”, to let it wait until it is fully loaded.

When working with local scripts, you will mostly always need to use WaitForChild in such cases.

1 Like

I know this is a stupid question, but where exactly in the localscript do I add it?

Try this code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CodesButton = script.Parent
local TweenService = game:GetService("TweenService")
local CodesFunction = ReplicatedStorage:WaitForChild("CodesFunction")
local Frame = script.Parent:WaitForChild("Frame")
local Redeem = Frame:WaitForChild("Redeem")
local TextBox = Frame:WaitForChild("TextBox")
local IsOpen = false

local player = game.Players.LocalPlayer

local CurrentTween = nil

CodesButton.MouseButton1Click:Connect(function()
	
	if CurrentTween then
		CurrentTween:Canel()
		CurrentTween = nil
	end

	local Tween = TweenService:Create(Frame, TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {
		Position = (IsOpen and UDim2.new(0.5, 0, 1.5, 0) or UDim2.new(0.5, 0, 0.5, 0))
	})
	CurrentTween = Tween
	Tween:Play()

	IsOpen = not IsOpen
end)


Redeem.MouseButton1Click:Connect(function() -- On Mouse click on the redeem button
	local Result = CodesFunction:InvokeServer(TextBox.Text) -- Invokes the server, sends the code that the player has put in the TextBox

	if Result == 3 then
		TextBox.PlaceholderText = "Code successfully redeemed!"
	elseif Result == 2 then
		TextBox.PlaceholderText = "Code expired!"
	elseif Result == 1 then
		TextBox.PlaceholderText = "Code invalid/ already redeemed!"
	end

	TextBox.Text = ""
	wait(2)
	TextBox.PlaceholderText = "Enter code here..."
end)
1 Like

It still gives me the errror
Screenshot_6

hold on, i think i know what the problem is now…

1 Like

Take your time!
I appreciate you helping me so much.

Try this code:

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local LPlayer = Players.LocalPlayer
local PlayerGUI = LPlayer:WaitForChild("PlayerGui")
local Main = PlayerGUI:WaitForChild("Main")
local CodesGUI = PlayerGUI:WaitForChild("CodesGui")
local Bar = Main:WaitForChild("Bar")
local CodesButton = Bar:WaitForChild("CodesButton")
local CodesFunction = ReplicatedStorage:WaitForChild("CodesFunction")
local Frame = CodesGUI:WaitForChild("Frame")
local Redeem = Frame:WaitForChild("Redeem")
local TextBox = Frame:WaitForChild("TextBox")
local IsOpen = false

local player = game.Players.LocalPlayer

local CurrentTween = nil

CodesButton.MouseButton1Click:Connect(function()
	
	if CurrentTween then
		CurrentTween:Canel()
		CurrentTween = nil
	end

	local Tween = TweenService:Create(Frame, TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {
		Position = (IsOpen and UDim2.new(0.5, 0, 1.5, 0) or UDim2.new(0.5, 0, 0.5, 0))
	})
	CurrentTween = Tween
	Tween:Play()

	IsOpen = not IsOpen
end)


Redeem.MouseButton1Click:Connect(function() -- On Mouse click on the redeem button
	local Result = CodesFunction:InvokeServer(TextBox.Text) -- Invokes the server, sends the code that the player has put in the TextBox

	if Result == 3 then
		TextBox.PlaceholderText = "Code successfully redeemed!"
	elseif Result == 2 then
		TextBox.PlaceholderText = "Code expired!"
	elseif Result == 1 then
		TextBox.PlaceholderText = "Code invalid/ already redeemed!"
	end

	TextBox.Text = ""
	wait(2)
	TextBox.PlaceholderText = "Enter code here..."
end)
1 Like

you’re attempting to insert a mousebutton1click event to a screengui.

1 Like

Thank you so so much!! It’s working perfectly!!

1 Like