Help with Stamina

Hello! So I have this script that I modified, and it works great for what I’m looking for. The thing is, it has a slight bug with the stamina bar.
How do I make it so that the stamina (ii) doesn’t go below 0 / the stamina doesn’t go past the left?

VIDEO:

SCRIPT:

while wait() do
	print(ii)
	if humanoid then
		if humanoid.MoveDirection:Dot(humanoid.MoveDirection) == 0 then moving = false else moving = true end
		if tired then 
			TS:Create(player.Character.Humanoid, TweenInfo.new(1.25), {WalkSpeed = NormalWalkSpeed}):Play()
			if not moving then
				ii = ii + 1
			end
			if ii > 0 then
				tired = false
				Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', .5, true)
			end
		elseif running == true then
			if moving then
				TS:Create(player.Character.Humanoid, TweenInfo.new(2), {WalkSpeed = NewWalkSpeed}):Play()
				ii = ii - .025
				Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', 1.5, true)
				if ii == 0 then
					tired = true
					ii = -1
				end
				if ii == 0 then tired = true end
			elseif ii < 10 then
				ii = ii + .035
				Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', .5, true)
			end
			if not moving then
				ii = ii + .035
				Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', 1.5, true)
			end
			
			if ii <= 0 then
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
			end
			if ii > runRef then
				Bar.BackgroundColor3 = Color3.fromRGB(222, 255, 202)
			else
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
			end
		else TS:Create(player.Character.Humanoid, TweenInfo.new(1.25), {WalkSpeed = NormalWalkSpeed}):Play()
			if not moving and ii < 10 then
				ii = ii + .035
				Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', .5, true)
			end
			
			if ii <= 0 then
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
			end
			if ii > runRef then
				Bar.BackgroundColor3 = Color3.fromRGB(222, 255, 202)
			else
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
			end
		end
	end
end
1 Like

This is is where math.clamp can be used

local amount   = .25
local minValue = 0
local maxValue = 100

ii = math.clamp(
    ii + amount, -- First Argument is your Number
    minValue,    -- The minimum value you dont want the number to pass
    maxValue     -- The maximum value you dont want the number to pass
)

You can Basically do the same with the Tween if you like.

UDim2.new(math.clamp(ii/10, 0, 1), 0, 1, 0) -- Should look like this
1 Like

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