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:
1 | 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
02 | if game.Workspace.LoadVal.Disabled = = true then |
03 | script.Parent.BackgroundTransparency = 0.9 |
04 | script.Parent.TextButton.TextTransparency = 0.9 |
06 | script.Parent.BackgroundTransparency = 0.8 |
07 | script.Parent.TextButton.TextTransparency = 0.8 |
09 | script.Parent.BackgroundTransparency = 0.7 |
10 | script.Parent.TextButton.TextTransparency = 0.7 |
12 | script.Parent.BackgroundTransparency = 0.6 |
13 | script.Parent.TextButton.TextTransparency = 0.6 |
15 | script.Parent.BackgroundTransparency = 0.5 |
16 | script.Parent.TextButton.TextTransparency = 0.5 |
18 | script.Parent.BackgroundTransparency = 0.4 |
19 | script.Parent.TextButton.TextTransparency = 0.4 |
21 | script.Parent.BackgroundTransparency = 0.3 |
22 | script.Parent.TextButton.TextTransparency = 0.3 |
24 | script.Parent.BackgroundTransparency = 0.2 |
25 | script.Parent.TextButton.TextTransparency = 0.2 |
27 | script.Parent.BackgroundTransparency = 0.1 |
28 | script.Parent.TextButton.TextTransparency = 0.1 |
30 | script.Parent.BackgroundTransparency = 0 |
31 | script.Parent.TextButton.TextTransparency = 0 |
and at the end of the code:
1 | 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.
02 | if game.Workspace.LoadVal.Disabled = = true then |
04 | script.Parent.BackgroundTransparency = i |
05 | script.Parent.TextButton.TextTransparency = i |
10 | game.Workspace.LoadVal.Changed:connect(onDisabled) |