Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I update the transparency of this GUI?

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.

3 comments

0

Is this in a local script? Any errors and where does the script hang? You should have a break somewhere to make sure it stops the loop. Preferably if stamina gets to 0.

0

stam is not a reference to the actual property of the StaminaValue. You'll need to write stam.Value for each time you want to get the value.

0

Or he could simply put the "stam" variable inside the loop :l

2 answers

✓ Accepted answer

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:-

  • can this local script run before the player spawns as Character or StaminaValue would be nil
  • if the player dies the new StaminaValue object is not going to be used and a new changed event is needed

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.

3 comments

0

Oh no, you answered one of his problems before mine was showen!

0

I think people are going to think that i copied you :[

0

It works now! Thank you so much. You're doing God's work.

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.

1 comment

0

use game.Players.LocalPlayer.CharacterAdded:Wait() instead of a repeat loop

This discussion is preserved as it was. New answers, comments, and votes are closed.