Is this the correct way to create a sprinting system?

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)
1 Like

use a while loop it works best in making sprinting systems

While loop kinda help it I guess?

Nevermind I just asked chatgpt and now my problems are solved.


	if shiftKeyDown and stamina <= 0 then
		return 
	end

	if stamina < maxStamina then
		stamina = math.min(stamina + 1, maxStamina)
		print("Inactive - Current Stamina: " .. stamina)
	end
	-- Reset walking speed to default when player stops moving
	if not shiftKeyDown and stamina >= maxStamina then
		walkingSpeed = 16
	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

			-- Reset walking speed to default when player stops moving or stops sprinting
			if not shiftKeyDown or not isMoving then
				walkingSpeed = 16
			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)

while wait() do
	updateMovement() 
	print("Current Stamina: " .. stamina)
	if stamina <= 0 and not shiftKeyDown then
		wait(2)
		if not shiftKeyDown then
			stamina = maxStamina
		end
	end
end```

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.