My code won't run once clicking a UI button

Basically my code won’t run after this line:

ui.Warning.Acknowledge.MouseButton1Down:Connect(function()

The full code:

local ui = game.StarterGui.Introduction


local TweenService = game:GetService("TweenService")
print ("hi completed.")
ui.Warning.Acknowledge.MouseButton1Down:Connect(function()
	print ("button completed.")
	for _, gui in pairs(ui:GetDescendants()) do
		print ("descend completed.")
		if not gui:IsA("TextLabel") or not gui:IsA("Frame") then continue end 
		print ("For completed.")
			local tweenInfo = TweenInfo.new(
				2, -- Time
				Enum.EasingStyle.Linear, -- EasingStyle
				Enum.EasingDirection.Out, -- EasingDirection
				-1, -- RepeatCount
				true, -- Reverses 
				0 -- DelayTime
			)
			TweenService:Create(gui, tweenInfo,{
			Transparency = 1
		}):Play()
	end
end)

Any help on this is much appreciated! :slight_smile:

I’m still learning so any advice given always helps, thank you for your time.

Are you sure your button “Acknowledge” is a TextButton? Instead try using MouseButton1Click event instead of MouseButton1Down, just for testing purposes, since I really can’t see the problem in the code?

1 Like

Hi, I’ve checked the button and it most certainly is! I am just as confused as you. I’ll try MouseButton1Click!

Are you running your code in a server script or local script? Server scripts can’t detect screen UI events.

1 Like

Local script! It is a client-side ui!

I think the problem is when you are defining your GUI. StarterGUI is just a Service, while you are supposed to get a PlayerGUI, which is directly from a Player.

Basically StarterGUI acts as a container which holds UI. Whenever a Player joins, that UI inside of StarterGUI will clone and be placed inside of the Player’s PlayerGUI, which is an another container, but only for that one Player!!!
I recommend you to do following:

-- Services --

local Players = game:GetService("Players")

-- Variables --

local Player = Players.LocalPlayer
local PlayerUI = Player.PlayerGui
local ui = PlayerUI.Introduction

Here is more about PlayerGUI and StarterGUI.

https://developer.roblox.com/en-us/api-reference/class/StarterGui
https://developer.roblox.com/en-us/api-reference/class/PlayerGui

2 Likes

This worked! Thank you very much :slight_smile:

I havent gone far into the code but this is spelled incorrectly.

1 Like

Oops, that is just a stupid error! Thanks for letting me know lol.

You have to reference the PlayerGui instead of the StarterGui, because StarterGui basically just clones everything from there into the PlayerGui when a player joins. Also, I’m not sure you can use “Transparency” to change the transparency of a GUI object, it’s an invalid property. So in this case, you would have to change the BackgroundTransparency for the TextLabels and Frames, and for TextLabels, you would have to change the TextTransparency.

New Code
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local PlayerGUI = Player.PlayerGui
local ui = PlayerGUI.Introduction
ui.Warning.Acknowledge.MouseButton1Down:Connect(function()
	for _, gui in pairs(ui:GetDescendants()) do
		if not gui:IsA("TextLabel") or not gui:IsA("Frame") then continue end 
		local tweenInfo = TweenInfo.new(
			2, -- Time
			Enum.EasingStyle.Linear, -- EasingStyle
			Enum.EasingDirection.Out, -- EasingDirection
			-1, -- RepeatCount
			true, -- Reverses 
			0 -- DelayTime
		)
		local goal = {
			BackgroundTransparency = 1
		}
		if gui:IsA("TextLabel") then
			goal["TextTransparency"] = 1
		end
		TweenService:Create(gui, tweenInfo, goal):Play()
	end
end)
1 Like

Thank you, this code still won’t run! I think the issue is here as it was on my prev code:
if not gui:IsA("TextLabel") or not gui:IsA("Frame") then continue end

Oh, you really just have to change the or keyword to and, the and keyword just adds another condition to check.

1 Like

This is what happens when you take a break from learning to code :face_with_diagonal_mouth: , also it now loops (on the tween) aha! Btw thank you for the help!

I don’t recommend creating Tweens in Events like MouseButton1Click, because every time you click on a button, you create a Tween, and that Tween is being stored somewhere as a memory address, which can eventually turn into a memory leak!

Solution: Create a Tween outside of an Event!

Also my button won’t fade, however the rest of the ui does.

So in future, do I use remote events?

No, you don’t have to use a remote event. Also, I fixed the entire script, the issue was that you were adding a repeat count and made the tween reverse.

New Code
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local PlayerGUI = Player:WaitForChild("PlayerGui")
local ui = PlayerGUI.Introduction
ui.Warning.Acknowledge.MouseButton1Down:Connect(function()
	for _, gui in pairs(ui:GetDescendants()) do
		if not gui:IsA("TextLabel") and not gui:IsA("Frame") then continue end 
		if gui:IsA("TextLabel") and gui.TextTransparency == 0 or gui:IsA("Frame") and gui.BackgroundTransparency == 0 then continue end
		local tweenInfo = TweenInfo.new(
			2, -- Time
			Enum.EasingStyle.Linear, -- EasingStyle
			Enum.EasingDirection.Out -- EasingDirection
		)
		local goal = {
			BackgroundTransparency = 1
		}
		if gui:IsA("TextLabel") then
			goal["TextTransparency"] = 1
		end
		TweenService:Create(gui, tweenInfo, goal):Play()
	end
end)

EDIT: I also made an edit to this post a couple mins later, so if you immediately tested this out before I edited this message, then try re-copying the script.

That has nothing to do with my explanation, this is specifically related to a client. Remote Events are used for Client - Server relationship.

Sorry, my bad! What do you mean by ‘Outside an Event’?

Actually I think you are fine, since you are not creating a variable for your Tween:Create() and you are playing it immediately, but in the future make sure to destroy your created Tweens when you don’t need to use them, which can prevent memory leak. You don’t need to worry about it right now, but in the near future, you might.