Why isn't this value updating?

Maybe there’s something I’m missing. I’m trying to change this IntValue (PowerValue) that’s a child of my sprinting GUI, so I could use it for tracking how much stamina Power is left. I followed some similar code that works, but for some reason, it doesn’t work for this script. Help?

Edit: changed the chopped up script to the complete one just in case it helps. Problem is most likely the code, because I’ve changed the value to a NumberValue and its still not doing anything.

local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')
local StarterGui = game:GetService("StarterGui")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local NormalWalkSpeed = 12
local NewWalkSpeed = 19 -- sprinting speed
local Power = 10 -- how much stamina there is, don't mess with value of 10

local PowerRepresent = Power
local PowerRepresent = script.Parent.PowerValue.Value

local PowerIncrease = script.Parent.PowerAddValue.Value  -- the amount of power being replenished when not running

if Player:FindFirstChild("IsSprinting") then -- finding or creating IsSprinting value
	sprinting = Player.IsSprinting
	print("Setup for IsSprinting value complete")
else
	Instance.new("BoolValue", Player).Name = "IsSprinting"
	Player.IsSprinting.Value = sprinting
	print("Created new IsSprinting value")
end

local sprinting = false
local IsSprinting = Player.IsSprinting
local CrouchingSpeed = script.Parent.CrouchHandler.CrouchingSpeed.Value

local IsCrouching = Player:WaitForChild("IsCrouching") -- other Player Statuses setup
local IsHealing = Player:WaitForChild("IsHealing")
local IsAiming = Player:WaitForChild("IsAiming")
local IsReloading = Player:WaitForChild("IsReloading")
local IsAimFiring = Player:WaitForChild("IsAimFiring")
local IsHipFiring = Player:WaitForChild("IsHipFiring")


repeat wait() until game.Players.LocalPlayer.Character

local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://913376220' -- sprint animation ID
RunAnim = Character.Humanoid:LoadAnimation(Anim)

UserInputService.InputBegan:connect(function(key, gameProcessed) 
	
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		if IsCrouching.Value == true or IsHealing.Value == true or IsAiming.Value == true or IsReloading.Value == true
		or IsAimFiring.Value == true or IsHipFiring.Value == true then 
			return
		end
		
		if Character.Humanoid.MoveDirection.Magnitude > 0 then
		RunAnim:Play()

		Character.Humanoid.WalkSpeed = NewWalkSpeed
		
		sprinting = true
		
		IsSprinting.Value = true
		
		while Power > 0 and sprinting do
		
			Power = Power - 0.03 -- the amount of stamina decreased when running
			UpdatePower(PowerRepresent)
			Bar:TweenSize(UDim2.new(Power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
			
			wait()

			if Power <= 0 then -- less than or equal to 0
				IsSprinting.Value = false
				Character:WaitForChild("SoundPart").HeavyBreathing:Play()
				RunAnim:Stop()
				Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0) -- changes color to red
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
				end
			end
		end	
	end
end)


UserInputService.InputEnded:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		if IsCrouching.Value == true then 
			return
		end
		
		if Character.Humanoid.MoveDirection.Magnitude > 0 then
		
		RunAnim:Stop()
		
		Character.Humanoid.WalkSpeed = NormalWalkSpeed
		
		sprinting = false
		
		IsSprinting.Value = false
		
		while Power < 10 and not sprinting do
			
			RunAnim:Stop()
			
			Power = Power + PowerIncrease 
			UpdatePower(PowerRepresent)
			Bar:TweenSize(UDim2.new(Power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
			
			wait()
			
			if Power <= 3 then -- less than or equal to 3
				RunAnim:Stop()
				sprinting = false
				IsSprinting.Value = false
			if IsCrouching.Value == true then
				Character.Humanoid.WalkSpeed = CrouchingSpeed		
				else	
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
					end
				end	
			end
		end
	end	
end)


local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	if Power >= 3 then -- greater than or equal to 3
		Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
		Character:WaitForChild("SoundPart").HeavyBreathing:Stop()
	end	
end)

RunService.RenderStepped:Connect(function()
	if Power <= 3 then -- less than or equal to 3
		Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0)
	end	
end)


function UpdatePower(value)
	script.Parent.PowerValue.Value = script.Parent.PowerValue.Value
	
	script.Parent.PowerValue.Value = PowerRepresent
	
end
2 Likes

What exactly is not working? The stamina display or the entirety of the code?

Also why do you need 2 variables with the same name?

1 Like

why are you setting the value to itself and then to the value object

1 Like

The IntValue that’s a child of the GUI isn’t changing its value, that’s not working. Yeah idk what I was doing, I was just following some code from a free model that seemed to work. What do I change or add to make it work though?

1 Like

It might be because you are using 0.03, and it is an Int Value. Int or Integer are whole numbers, such as 1,2,3,4,5, so you cannot have a number like 0.97. This might be the reason.

Would using a NumberValue or any other value work? Or is it just something wrong with the code?

I never really tried it, but maybe yes. But it’s not really related to the code itself. Another option is to increase the values by x100, to make them all integers.

Doesn’t seem to work even once I changed the IntValue to a NumberValue. Maybe its not updating because I’m looking at it in the Players GUI section?

This reply could help you understand on why it’s not working:

What happens if you increase everything by x100? For example if before you had 5 stamina, change it to 500, and reduce 3 instead of 0.03.

If this doesnt do any difference, it’s not related to what I’m talking about.

Did nothing except make the bar extend out off the screen, and the value still didn’t change. I’m guessing its something flawed with the code. Would posting the entire script help?

The bar extended because it is probably set to calculate the percentage from the old value, actually, this script is on the client right? You will need to update the value on the server not on the client.

PowerRepresent should point to the PowerValue and not to the current value. By doing this, PowerRepresent becomes a simple variable.

--wrong
local PowerRepresent = script.Parent.PowerValue.Value
--correct
local PowerRepresent = script.Parent.PowerValue

Getting PowerRepresent value should happen when you are updating power:

--old
UpdatePower(PowerRepresent)
--new
PowerRepresent.Value = Power

Which will make UpdatePower() function unnecessary.

1 Like

Thanks so much bro appreciate it, been stuck for a few days already.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.