Need help on stamina script

so basically i am making a stamina for punching system but there are some error in it that i dont know how to fix here is my whole script:

Player = game.Players.LocalPlayer
repeat wait() until Player.Character
Character = Player.Character
mouse = Player:GetMouse()
local power = 10
local debounce = true
local Bar = Player.PlayerGui.ScreenGui.Background.Bar
Enabled = true


mouse.Button1Down:connect(function()
		if Enabled == true and power > 0 then
			script.Function:FireServer("Combat", Character.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p)
			wait(0.45)
			power = power - 2
			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
			Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 0, 0), 0.001)
			print(power)
		end
end)


	if power < 0 then ---not working
		power = 0
	end


while power < 10 do --- not working
	wait()
	power = power + 0.3
	Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
	Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 0, 0), 0.001)	
end 

literally where i need help is where i put the comment the first one is not working like when i spam click it goes to negative number and is not return to 0

the second one is its not actually working idk why but it should be as there are no mistake into it. Any help would be grateful

if u didnt understand anything tell me

The while loop doesn’t function as that while loop will only be run through once when the script first boots up, and in that scenario the power is set to 10, so the while loop doesn’t actually run as power is not less than 10.

To fix that second loop your best bet is to change it to a while wait loop and then have an if statement in that loop checking for power to be less than 10.

To fix that first error, I’d suggest moving that if statement into the while loop as well as that is also only run through once at the start and will be ignore as power is set to 10 when that occurs.

1 Like

like can u give me a script example

Player = game.Players.LocalPlayer
repeat wait() until Player.Character
Character = Player.Character
mouse = Player:GetMouse()
local power = 10
local debounce = true
local Bar = Player.PlayerGui.ScreenGui.Background.Bar
Enabled = true


mouse.Button1Down:connect(function()
		if Enabled == true and power > 0 then
			script.Function:FireServer("Combat", Character.HumanoidRootPart.CFrame*CFrame.new(0,0,-2).p)
			wait(0.45)
			power = power - 2
			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
			Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 0, 0), 0.001)
			print(power)
		end
end)

while wait() do --- not working
	if power < 0 then ---not working
		power = 0
	end
    if power < 10 then
	    power = power + 0.3
    	Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
    	Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 0, 0), 0.001)
    end	
end
1 Like

oh wow thank for the great help dude!

but there a problem. the problem is that the first code isnt working its still being a negative number and its breaking the second code the second was working fine until i put the first code

oh nvm i fixed it