So basically,
I have a script in a PlayerGUI and I want the GUI to disappear when a script in the work space becomes disabled.
The code looks like this.
if game.Workspace.LoadVal.Disabled == true then script.Parent.BackgroundTransparency = 0.9 script.Parent.TextButton.TextTransparency = 0.9 wait(0.1) script.Parent.BackgroundTransparency = 0.8 script.Parent.TextButton.TextTransparency = 0.8 wait(0.1) script.Parent.BackgroundTransparency = 0.7 script.Parent.TextButton.TextTransparency = 0.7 wait(0.1) script.Parent.BackgroundTransparency = 0.6 script.Parent.TextButton.TextTransparency = 0.6 wait(0.1) script.Parent.BackgroundTransparency = 0.5 script.Parent.TextButton.TextTransparency = 0.5 wait(0.1) script.Parent.BackgroundTransparency = 0.4 script.Parent.TextButton.TextTransparency = 0.4 wait(0.1) script.Parent.BackgroundTransparency = 0.3 script.Parent.TextButton.TextTransparency = 0.3 wait(0.1) script.Parent.BackgroundTransparency = 0.2 script.Parent.TextButton.TextTransparency = 0.2 wait(0.1) script.Parent.BackgroundTransparency = 0.1 script.Parent.TextButton.TextTransparency = 0.1 wait(0.1) script.Parent.BackgroundTransparency = 0 script.Parent.TextButton.TextTransparency = 0 end
Is there an issue with this code that I have overlooked? Or is there maybe an issue with the script disabling?
Cheers,
Michael
EDIT: The script disables fine. It's this part that doesn't work.
Maybe you should try something like this:
Your Problem:The script keeps triggering right when the game starts.... only once! Which probably at the time the script doesn't needto trigger.
Steps To Fix It (2):
1) Remove your 1 time if statement
2) Replace it with a repeat wait() until.... Your Check
Below is the fixed code:
repeat wait() until game.Workspace.LoadVal.Disabled == true -- This will keep the code from continuing until LoadVal is disabled. -- Below is what you want to happen when it does. script.Parent.BackgroundTransparency = 0.9 script.Parent.TextButton.TextTransparency = 0.9 wait(0.1) script.Parent.BackgroundTransparency = 0.8 script.Parent.TextButton.TextTransparency = 0.8 wait(0.1) script.Parent.BackgroundTransparency = 0.7 script.Parent.TextButton.TextTransparency = 0.7 wait(0.1) script.Parent.BackgroundTransparency = 0.6 script.Parent.TextButton.TextTransparency = 0.6 wait(0.1) script.Parent.BackgroundTransparency = 0.5 script.Parent.TextButton.TextTransparency = 0.5 wait(0.1) script.Parent.BackgroundTransparency = 0.4 script.Parent.TextButton.TextTransparency = 0.4 wait(0.1) script.Parent.BackgroundTransparency = 0.3 script.Parent.TextButton.TextTransparency = 0.3 wait(0.1) script.Parent.BackgroundTransparency = 0.2 script.Parent.TextButton.TextTransparency = 0.2 wait(0.1) script.Parent.BackgroundTransparency = 0.1 script.Parent.TextButton.TextTransparency = 0.1 wait(0.1) script.Parent.BackgroundTransparency = 0 script.Parent.TextButton.TextTransparency = 0
Usually when you are working on a "do this when this happens" type of thing, you are going to rely on Events and ways to handle the events.
What are events? They are basically things that are reported to the Roblox Server when something happens
For this case, we are going to focus on the Changed
Event which fires when a property of the object is changed.
In order to listen to an event, you need to construct an event handler. The basic form looks like this:
Object.Event:connect(functionYouWantToPerform)
This basically tells us, when an Object
fires the Event
, connect
the code with this function
. The Object is basically the thing you are referencing (game.Workspace.LoadVal
), and it has various events which could fire, such as Changed, AncestryChanged, ChildAdded, etc.. Look at the ContextHelp button in roblox studio for a list of every event for an object
Because an event handler connects a code with a function, you will have to make the action you are trying to perform into a function. It literally can be simple as adding the words function onDisabled()
and end
onDisabled is the name of the function. you can name it in fact anything you want, as long as it is a valid identifier.
For example
function onDisabled() if game.Workspace.LoadVal.Disabled == true then script.Parent.BackgroundTransparency = 0.9 script.Parent.TextButton.TextTransparency = 0.9 wait(0.1) script.Parent.BackgroundTransparency = 0.8 script.Parent.TextButton.TextTransparency = 0.8 wait(0.1) script.Parent.BackgroundTransparency = 0.7 script.Parent.TextButton.TextTransparency = 0.7 wait(0.1) script.Parent.BackgroundTransparency = 0.6 script.Parent.TextButton.TextTransparency = 0.6 wait(0.1) script.Parent.BackgroundTransparency = 0.5 script.Parent.TextButton.TextTransparency = 0.5 wait(0.1) script.Parent.BackgroundTransparency = 0.4 script.Parent.TextButton.TextTransparency = 0.4 wait(0.1) script.Parent.BackgroundTransparency = 0.3 script.Parent.TextButton.TextTransparency = 0.3 wait(0.1) script.Parent.BackgroundTransparency = 0.2 script.Parent.TextButton.TextTransparency = 0.2 wait(0.1) script.Parent.BackgroundTransparency = 0.1 script.Parent.TextButton.TextTransparency = 0.1 wait(0.1) script.Parent.BackgroundTransparency = 0 script.Parent.TextButton.TextTransparency = 0 end end
and at the end of the code:
game.Workspace.LoadVal.Changed:connect(onDisabled)
One more thing. You aren't doing this very efficiently in the first place. If you want to do the same thing over and over again in a scaled way, the numeric for loop is the place to go. Look it up if you can.
It condenses the code to be this small.
function onDisabled() if game.Workspace.LoadVal.Disabled == true then for i = .9, 0, -.1 do script.Parent.BackgroundTransparency = i script.Parent.TextButton.TextTransparency = i wait(0.1) end end end game.Workspace.LoadVal.Changed:connect(onDisabled)