I’m making a script where when a value decreases to 35 or below, a GUI starts becoming opaque.
Doesn’t work.
The script is below.
Air = game.Players.LocalPlayer.Character.Water.Air
TransparencyObj = script.Parent.LowOxygenEffect
while true do
if Air.Value <= 35 then
TransparencyObj.BackgroundTransparency = Air.Value / 35
wait(0)
elseif Air.Value >= 35 then
TransparencyObj.BackgroundTransparency = 1
end
end
what’s the problem? instead of becoming opaque, what happens to it? also try adding print() statements in the if else statement to see what part is running.
sidenote: you can remove the second if statement, it can just be else since there’s nothing else
sidenote 2: while true do can lag stuff out, try adding a wait() somewhere in there
Air = game.Players.LocalPlayer.Character.Water.Air
TransparencyObj = script.Parent.LowOxygenEffect
while true do
wait(0.001) -- wait(0) will cause a problem
if Air.Value <= 35 then
TransparencyObj.BackgroundTransparency = Air.Value / 35
else -- replaced "elseif Air.Value >= 35 then" with "else" since it does basically the same thing.
TransparencyObj.BackgroundTransparency = 1
end
end
But this would be very ineffective since you are checking the value every millisecond or less instead of when the value changes.
If Air is something like a IntValue object (which I am assuming it is based on your code), then using while true do would be inefficient. Instead, I would use the Changed event. This way, you can only check if the value has been changed and not every single millisecond.
Air = game.Players.LocalPlayer.Character.Water.Air
TransparencyObj = script.Parent.LowOxygenEffect
Air.Changed:connect(function(value) -- "value" is the new value of Air.
if value <= 35 then
TransparencyObj.BackgroundTransparency = value / 35
else
TransparencyObj.BackgroundTransparency = 1
end
end)
Hope this helps!
Also, I want to mention this, but if you still want to use elseif Air.Value >= 35 then, then you need to replace >= with > since the case when Air.Value is equal to 35 is already taken care of in the if conditional. If you didn’t do that, then an error would occur when the value is equal to 35 since the game cannot tell which section of to run since there are two conditionals that check for the same condition (i.e. Air.value == 35) and each leads to different pieces of code. I want you to keep that in mind.
It still doesn’t work. Is it something with the division? Because if the air value was 35 and it was divided by itself, it would be transparent by 1. It would then decrease.
There might be problem with how you are determining the transparency of the object. I recommend experimenting with different methods until you find one that fits.
I would recommend looking here for ideas (I would recommend the math.max function).
They are (i.e. represent) the same exact value, so it shouldn’t really matter. If you are struggling, I recommend looking at the documentation for the Changed Event:
Oh, it’s the changed value. I see. Do you have a way of making it so the transparency of the GUI is determined by how much the air value is? For example, if the air was 17.5, the transparency of the GUI would be 50%.
Air.Changed:connect(function(value) -- "value" is the new value of Air.
TransparencyObj.BackgroundTransparency = math.min(1, (0.5/17.5)*value)
end)
the math.min function, which I should have recommended to you earlier instead of math.max (Whoops!), is used to ensure that the value does grow bigger than one.
Other than that, I used a linear equation to determine the transparency in a way such that, if Air.Value is 17.5, then the transparency would be 0.5 (i.e. 50%). Also, (0.5/17.5)*35 = 1 so you do not need the if-else statement.
Hmmm… I am probably missing key details regarding how Air.value is determined. The equation I have goes under the assumption that if Air.Value is equal to zero, then the transparency value is also zero, which I could be very wrong about. Other than that I recommend to keep modifying the code until it does what you need it to do.
I want to see what the code looks like for you. Then I want to have you print of the value of value so I can see if there are any problems on that side. There might be a problem with some other code and not that code in specific.