Stamina script problem

this is my stamina script and the problem is if my stamina is 0 but it still running how do i fix this?

local player = game:GetService("Players").LocalPlayer
local camera = game:GetService("Workspace").CurrentCamera

local uis = game:GetService("UserInputService")
local tween = game:GetService("TweenService")
local module = require(game:GetService("ReplicatedStorage"):WaitForChild("Tween"))
local rs = game:GetService("RunService")

local stamina = 100
local sprint = false


uis.InputBegan:connect(function(input,typing)
	local char = player.Character or player.CharacterAdded:Wait()
	if typing then return end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if stamina >= 1 then
			print("Sprinting")
			sprint = true
			tween:Create(camera,module.run,{FieldOfView = 80}):Play()
			char.Humanoid.WalkSpeed = 35
		end
	end
end)

uis.InputEnded:Connect(function(input,typing)
	local char = player.Character or player.CharacterAdded:Wait()
	if typing then return end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		print("Walking")
		sprint = false
		tween:Create(camera,module.walk,{FieldOfView = 70}):Play()
		char.Humanoid.WalkSpeed = 16
	end
end)

rs.Heartbeat:Connect(function(Deltatime)
	if sprint then
		print(stamina)
		if stamina > 0 then
			stamina = stamina - 10 * Deltatime
		end
	end
end)

It’s best to use changed event, make a int value and put the numbers inside of there.

and you can detect if it goes 0 and under.

1 Like