Stamina not restoring/adding

hello!

  1. What do you want to achieve? Keep it simple

  2. What is the issue? its not restoring/adding the 0.3 to the value

  3. What solutions have you tried so far? asked my friend he said its fine, changed the code a bit

heres the code

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local RunS = game:GetService("RunService")

local Plr = Players.LocalPlayer
local Chr = Plr.Character or Plr.CharacterAdded:Wait()

local Anims = RS.Animations
local RunTrack = Chr.Humanoid.Animator:LoadAnimation(Anims.RunAnim)

local Main = StarterGui.Main
local StaminaUI = Main.StaminaUI
local StaminaValue = Main.StaminaValue

local Running = false

UIS.InputBegan:Connect(function(Input)
	if StaminaValue.Value < 5 then return end
	
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		
		Chr.Humanoid.WalkSpeed = 30
		Chr.Humanoid.JumpPower = 0
		
		Running = true
		
		RunTrack:Play()
		
	end
	
end)

UIS.InputEnded:Connect(function(Input)

	if Input.KeyCode == Enum.KeyCode.LeftShift then

		Chr.Humanoid.WalkSpeed = 16
		Chr.Humanoid.JumpPower = 35
		
		Running = false
		
		RunTrack:Stop()

	end

end)

while wait(0.1) do
	print(Running)
	print(StaminaValue.Value)

	if Running == true then
		StaminaValue.Value -= 1
	else
		StaminaValue.Value += 0.3
	end

	if StaminaValue.Value < 5 then
		Chr.Humanoid.WalkSpeed = 16
		RunTrack:Stop()
	end
	
end

thank you!

1 Like

is stamina value an intvalue? you would need to use a number value to add floats to it

2 Likes

IT WAS THAT SIMPLE?

let me try

now it has alot of numbers

image

Yea that is normal, has something to do with floating point approximation errors, quite complicated. Research it if you want, but it should really not matter since it is so small. If you do want to get rid of that small error though, you can do this:

local Value = 147.3000000000005
local MultipliedValue = Value*10 --1473.000000000005
local RoundedValue = math.round(MultipliedValue) --1473
local FinalValue = RoundedValue/10 --147.3

local FinalValue = math.round(147.3000000000005*10)/10 --Same value

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