I’ve been trying to create a sprinting and stamina system. It seems to work a little, but when I short press shift it seems like it gave me the runningspeed instead of walkingspeed
edit :
Here’s the code:
local RunService = game:GetService("RunService")
local shiftKeyDown = false
local stamina = 100
local maxStamina = 100
local walkingSpeed = 16
local runningSpeed = 60
local function checkActivated()
if stamina > 0 then
walkingSpeed = runningSpeed
stamina = stamina - 1
print("Activated - Current Stamina: " .. stamina)
else
walkingSpeed = 16
print("Stamina depleted! Running speed reduced.")
end
end
local function checkInactive()
if shiftKeyDown and stamina <= 0 then
return
end
if stamina < maxStamina then
stamina = math.min(stamina + 1, maxStamina)
print("Inactive - Current Stamina: " .. stamina)
end
end
local function updateMovement()
local player = game.Players.LocalPlayer
local character = player.Character
if character then
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local isMoving = humanoid.MoveDirection.Magnitude > 0
if shiftKeyDown and isMoving then
checkActivated()
else
checkInactive()
end
humanoid.WalkSpeed = walkingSpeed
end
end
end
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
shiftKeyDown = true
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
shiftKeyDown = false
end
end)
game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
updateMovement() -- Check initial movement state when the character is added
end)
RunService.Heartbeat:Connect(function()
updateMovement()
print("Current Stamina: " .. stamina)
if stamina <= 0 and not shiftKeyDown then
wait(2)
if not shiftKeyDown then
stamina = maxStamina
end
end
end)