GUI won't tween onto screen when a part is clicked, what's wrong with my code?

I am trying to make my GUI tween onto the screen, however, I cannot seem to find the issue in my code. There are no errors in the output, when tested with print statements the code itself ran just fine. I have tried using both script types, as well as, moving them into the ClickDetector and adjusting the code as needed. Also, the Frame’s Visible property is set to True when viewing the properties tab.
This all leads to the same result of the GUI not being visible to the player.

Here’s my code:

local clickDetector = game.Workspace.Portal.BuyDoor.ClickDetector

function onMouseClick()
	if game.StarterGui.PortalGui.Open.Value == false then
		game.StarterGui.PortalGui.Open.Value = true
		game.StarterGui.PortalGui.Frame:TweenPosition(UDim2.new(0.5, -200,0.5, -125),"Out","Quint",1,true)
	else
		game.StarterGui.PortalGui.Open.Value = false
		game.StarterGui.PortalGui.Frame:TweenPosition(UDim2.new(0.5, -200,-0.5, -125),"Out","Quint",1,true)
	end
end

clickDetector.MouseClick:Connect(onMouseClick)

Here’s two screenshots of where everything is located (let me know if you need more):

image

image

I tried searching for other posts on the forum possibly similar to mine, but did not find any that were helpful to me. I also did a little digging on the Dev Hub to get some information on ClickDetectors, but no use to my knowledge.

Side note: This is my first post for a topic, so if I’ve done anything wrong please be sure to let me know. Thank you!

You should be referencing the GUI in the player’s PlayerGui folder, not the one in StarterGui. To do this simply change your code to:

local Players = game:GetService("Players")

local gui = Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("PortalGui")
local clickDetector = workspace:WaitForChild("Portal"):WaitForChild("BuyDoor"):WaitForChild("ClickDetector")

function onMouseClick()
	if gui.Open.Value == false then
		gui.Open.Value = true
		gui.Frame:TweenPosition(UDim2.new(0.5, -200,0.5, -125),"Out","Quint",1,true)
	else
		gui.Open.Value = false
		gui.Frame:TweenPosition(UDim2.new(0.5, -200,-0.5, -125),"Out","Quint",1,true)
	end
end

clickDetector.MouseClick:Connect(onMouseClick)

This should be in a local script.

3 Likes

To follow up on what @RetributionVI said, StarterGui is just a storage folder that gets cloned to the player’s PlayerGui folder when they spawn in. The MouseClick event actually returns the player as an argument, so you can edit the line defining the function to accept the argument:

function onMouseClick(player)

Then, to reference the GUI, you can just reference it inside of the PlayerGui folder:

if player.PlayerGui.PortalGui.Open.Value==false then
--and so on
2 Likes

I see, I will test that out shortly and be sure to update this.

Also, thank you @ChipioIndustries for the explanation. That makes so much more sense.

Alright, so I’ve made the changes to the local script and tested it out but nothing.

Now before continuing, I want to ensure I got the second part of what you said right so correct me if I’m wrong. If I understood it correctly, you were saying to edit line 7 (the if statement) to what you put so it could reference the PlayerGui folder?

You’ll need to edit any reference to the PortalGui to use the PlayerGui path instead. Basically, just change any instance of game.StarterGui to player.PlayerGui.

I did that, yet still no success. So I am at a loss.

@RetroGaming102 You need to take the player value from the ClickDetector and pass it through the function like this:

local GUI_NAME = "PortalGui"
local ClickDetector = game.Workspace.ClickDetector -- or where you ClickDetector is

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
    local p = PlayerWhoClicked
    gui = p.PlayerGui:WaitForChild(GUI_NAME)
    if gui.Open.Value == false then
		gui.Open.Value = true
		gui.Frame:TweenPosition(UDim2.new(0.5, -200,0.5, -125),"Out","Quint",1,true)
	else
		gui.Open.Value = false
		gui.Frame:TweenPosition(UDim2.new(0.5, -200,-0.5, -125),"Out","Quint",1,true)
	end   
end)

@RetroGaming102 Also you are running it on a script so the LocalPlayer function does not work, to use LocalPlayer you must be using a LocalScript.

I tried your way and ended with the same results as before.

As for your other comment, I do have it in a LocalScript. I’ll show you the current code I have.

local Players = game:GetService("Players")

local Gui = Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("PortalGui")
local clickDetector = workspace:WaitForChild("Portal"):WaitForChild("BuyDoor"):WaitForChild("ClickDetector")

function onMouseClick(player)
	if Gui.Open.Value == false then
		Gui.Open.Value = true
		Gui.Frame:TweenPosition(UDim2.new(0.5, -200,0.5, -125),"Out","Quint",1,true)
	else
		Gui.Open.Value = false
		Gui.Frame:TweenPosition(UDim2.new(0.5, -200,-0.5, -125),"Out","Quint",1,true)
	end
end

clickDetector.MouseClick:Connect(onMouseClick)

Are there any errors shown in the output box when you try it out?

I’ve made a few changes to my code (use it in a normal Script):

local GUI_NAME = "PortalGui"
local ClickDetector = game.Workspace:WaitForChild("Portal").BuyDoor.ClickDetector -- or where you ClickDetector is

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
    local p = PlayerWhoClicked
    local gui = p.PlayerGui:WaitForChild(GUI_NAME)
    if gui.Open.Value == false then
		gui.Open.Value = true
		gui.Frame:TweenPosition(UDim2.new(0.5, -200,0.5, -125),"Out","Quint",1,true)
	else
		gui.Open.Value = false
		gui.Frame:TweenPosition(UDim2.new(0.5, -200,-0.5, -125),"Out","Quint",1,true)
	end   
end)

I tested it and it works, the reason why your code isn’t working is because you can’t define a LocalPlayer through the workspace, only through ScreenGuis, StarterPlayer and StarterCharacter.

1 Like

Adding on to what @Lodok3YT has stated above, a local script will only run if it is located in:

  • a player’s Backpack
  • a player’s character model
  • a player’s PlayerGui
  • a player’s PlayerScripts
  • the ReplicatedFirst service

This is stated in the roblox developer wiki. I recommend you move the local script under the GUI object and the code which several people above this post have provided will work.

6 Likes

Worked like a charm, I also learned something pretty valuable so that’s always good. Thank you for your help, it is very much appreciated!

Also, thank you to @Lodok3YT & @ChipioIndustries for taking the time to help as well.

That didn’t work for me.

   local Players = game:GetService("Players")

local gui = Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("YouTube")
local clickDetector = workspace:WaitForChild("YouTubeHandler"):WaitForChild("ClickDetector")

function onMouseClick()
	if gui.Open.Value == false then
		gui.Open.Value = true
		gui.TextButton:TweenPosition(UDim2.new(0.447, 0, 0.822, 0), "Out", "Bounce", 1.5)
	else
		gui.Open.Value = false
		gui.TextButton:TweenPosition(UDim2.new(0.447, 0, 1.5, 0), "Out", "Bounce", 1.5)
	end
end

clickDetector.MouseClick:Connect(onMouseClick)