What do you want to achieve?
I’m trying to make a sprint system + stamina system
What is the issue?
If I stop holding LeftShift and the player still has stamina, it will force the player to sprint until the stamina reaches 0.
Local script:
local cs = game:GetService('ContextActionService')
local ts = game:GetService('TweenService')
local players = game:GetService('Players')
local localplayer = players.LocalPlayer
local character = localplayer.Character
local humanoid = character:WaitForChild('Humanoid')
local bar = script.Parent:FindFirstChild('bar')
local config = script.Parent:FindFirstChild('staminaConfig')
local tweenInfo = TweenInfo.new(
config.tweenTime.Value,
Enum.EasingStyle.Quint,
Enum.EasingDirection.Out,
0,
false
)
local loopPlaying = false
local function staminaHandler(continuee:boolean)
local cour = coroutine.create(function()
while task.wait(config.time.Value) do
if config.staminaCapacity.Value > 0 then
print('Loop is playing')
loopPlaying = true
config.staminaCapacity.Value -= config.removePerSecond.Value
else
loopPlaying = false
print('Loop stopped')
break
end
end
end)
local regen = coroutine.create(function()
while task.wait(config.regenDelay.Value) do
if config.staminaCapacity.Value == 100 then
print('stamina full')
break
else
print('regenerating')
loopPlaying = true
config.staminaCapacity.Value += config.removePerSecond.Value
end
end
end)
if continuee then
coroutine.close(regen)
coroutine.resume(cour)
else
coroutine.close(cour)
coroutine.resume(regen)
loopPlaying = false
end
end
local ogWalkspeeed = humanoid.WalkSpeed
local function sprintOn()
local runTween = ts:Create(humanoid,tweenInfo,{['WalkSpeed'] = humanoid.WalkSpeed + config.sprintSpeed.Value})
runTween:Play()
end
local function sprintOff()
local runTween = ts:Create(humanoid,tweenInfo,{['WalkSpeed'] = ogWalkspeeed})
runTween:Play()
end
local function inputFunction(actionName, inputState, inputObject)
if actionName == 'sprint' then
if inputState == Enum.UserInputState.Begin then
if config.staminaCapacity.Value >= 0 then
sprintOn()
staminaHandler(true)
while task.wait() do
if config.staminaCapacity.Value <= 0 or inputState == Enum.UserInputState.End then
sprintOff()
staminaHandler(false)
break
end
end
end
end
end
end
cs:BindAction('sprint',inputFunction,true,Enum.KeyCode.LeftShift)
cs:SetTitle('sprint','Sprint')