this script is supposed to be a sprint system with stamina. the stamina sort of works but has problems.
- after reaching 0 stamina and you start sprinting again. for a certain amount of time when you run your stamina will still regenerate and not decrease
- sometimes the stamina just freezes (no idea why)
no errors pop up in the developer console so i dont know where it goes wrong
local userinputservice = game:GetService('UserInputService')
local tweenservice = game:GetService('TweenService')
local plr = game:GetService("Players").LocalPlayer
local char = workspace:WaitForChild(plr.Name)
local hum = char:WaitForChild('Humanoid')
local camera = workspace.CurrentCamera
local plrgui = plr.PlayerGui
local staminaGUI = plrgui.mainPlayerGui.Frame.TextLabel
local sprintanim = 17713474483
local runningSpeed = 35
local normalSpeed = 12
local fieldOfView = 80
local runAnimation = Instance.new('Animation')
runAnimation.AnimationId = 'rbxassetid://'..sprintanim
local rAnimation = hum:LoadAnimation(runAnimation)
local running = false
local stamina = 100
local regen = true
local regencooldown = 0.15
local losecooldown = 0.05
local tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
userinputservice.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.UserInputType == Enum.UserInputType.Keyboard then
if userinputservice:IsKeyDown(Enum.KeyCode.W) and userinputservice:IsKeyDown(Enum.KeyCode.LeftShift) and stamina > 0 then
hum.WalkSpeed = runningSpeed
tweenservice:Create(camera, tweeninfo, {FieldOfView = fieldOfView}):Play()
running = true
regen = false
end
end
end)
userinputservice.InputEnded:Connect(function(input, isTyping)
if isTyping then return end
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.W then
hum.WalkSpeed = normalSpeed
tweenservice:Create(camera, tweeninfo, {FieldOfView = 70}):Play()
running = false
regen = false
end
end
end)
hum.Running:Connect(function(speed)
if speed >= 10 and running and not rAnimation.IsPlaying and stamina > 0 then
rAnimation:Play()
hum.WalkSpeed = runningSpeed
tweenservice:Create(camera, tweeninfo, {FieldOfView = fieldOfView}):Play()
regen = false
elseif speed >= 10 and not running and rAnimation.IsPlaying and stamina > 0 then
rAnimation:Stop()
hum.WalkSpeed = normalSpeed
tweenservice:Create(camera, tweeninfo, {FieldOfView = 70}):Play()
coroutine.wrap(function()
task.wait(1)
if not running then
regen = true
end
end)()
elseif speed < 10 and rAnimation.IsPlaying and stamina > 0 then
rAnimation:Stop()
hum.WalkSpeed = normalSpeed
tweenservice:Create(camera, tweeninfo, {FieldOfView = 70}):Play()
coroutine.wrap(function()
task.wait(1)
if not running then
regen = true
end
end)()
end
end)
hum.Changed:connect(function()
if hum.Jump and rAnimation.IsPlaying then
rAnimation:Stop()
end
end)
while true do
if regen and stamina < 100 then
stamina += 1
task.wait(regencooldown)
elseif running and stamina > 0 then
stamina -= 1
task.wait(losecooldown)
end
if stamina > 100 then
stamina = 100
elseif stamina <= 0 then
regen = false
stamina = 0
running = false
rAnimation:Stop()
hum.WalkSpeed = normalSpeed
tweenservice:Create(camera, tweeninfo, {FieldOfView = 70}):Play()
coroutine.wrap(function()
task.wait(5)
regen = true
end)()
end
staminaGUI.Text = stamina
task.wait()
end