I made all kinds of adjustments to this bit of code and nothing so far has worked. I want the textbox (DepletingMeter) to update and become more transparent as the "stam" value decreases and less transparent as it increases. The adjustments I've tried to make either cause the script to hang or just not function the way I'd like it to.
Here's my code.
local stam = game.Players.LocalPlayer.Character.StaminaValue.Value wait(1) while true do script.Parent.StaminaDisplay.Text = "Super Sprint: ".. stam wait(0.01) if stam < 100 then script.Parent.DepletingMeter.BackgroundTransparency = 1-(stam/100) end end
Thank you in advance.
In programming you should always use events over loops and a value holder includes a Changed event which passes the new value as a parameter.
The second error in this code is very common as you are getting the value of StaminaValue and not the StaminaValue value object. This results in the value being the same as you never access the new value of the value object. This is also solved by using the changed event.
Example
-- is it good practice to use GetService as the name can change local staminaValue = game:GetService("Players").LocalPlayer.Character.StaminaValue local scrPar = script.Parent local staminaDisplayText = scrPar.StaminaDisplay local depletingMeter = scrPar.DepletingMeter -- connect the changed event staminaValue.Changed:Connect(function(newVal) if newVal > 100 then return end -- do not run code if value larger than 100 staminaDisplayText .Text = "Super Sprint: " .. tostring(newVal) depletingMeter.BackgroundTransparency = 1-(newVal/100) end)
This example may not work as intended as there are a lot of things that need to be considered like:-
There are a lot of ways this can be done one of which is to use the TweenService to smoothly transition to the new background transparency.
Hope this helps.
Hello, NEXIUS10!
Your SCRIPT had some problems, and i have fixed those, but there was many other problems that i found and i managed to solve it;
1 - I believe that you have used an IntValue instead of a NumberValue, what is the difference?
Well, you cannot get a number like ( 0.01152 ) on an IntValue, but you can get that number on a NumberValue.
2 - You didn't update the "stam" variable, it would always stay the same value as the old value was.
3 - You didn't make sure that the "StaminaValue" DOES exist...
And i have solved all of those problems for you, and here is the new version of your script!
-- Note : Make sure to change the "StaminaValue"'s class into a NumberValue. repeat wait(0.02) until game.Players.LocalPlayer.Character while true do local stam = game.Players.LocalPlayer.Character:WaitForChild("StaminaValue").Value script.Parent.StaminaDisplay.Text = "Super Sprint: ".. stam wait() if stam < 100.0001 then script.Parent.Parent.DepletingMeter.BackgroundTransparency = stam/100 end end
[ EXTRA ]
If you want the textbox to lose transparency when the "StaminaValue" is increasing, you have to add an extra value for the "StaminaDisplay" 's text, while decreasing "StaminaValue" 's Value.