Sprint mechanic not working intendedly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    for the sprinting mechanic to stop once exausted, wait, refil, sprint, stop, wait, so on…
  2. What is the issue? Include screenshots / videos if possible!
    exausted mechanic only works once
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    plenty of looking on devforums
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
--{Services}
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

-- {{Variables}}
--Player
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Tweenservice = game:GetService("TweenService")
local camera = workspace.CurrentCamera


--Settings
local stamina = 100
local maxStamina = 100
local drainRate = 50
local refreshRate = 20

-- Booleans
local sprintHeld = false
local sprinting = false
local exhausted = false

-- { functions }

local function sprint(active)
	if exhausted then return wait() end
	sprinting = active
end

local function onInput(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and input.UserInputType ~= Enum.UserInputType.Gamepad1 then
		if humanoid.MoveDirection.Magnitude > 0 then
			sprintHeld = input.UserInputState == Enum.UserInputState.Begin
			sprint(sprintHeld)
		end	
	end
end

local function updateStaminaUI()
	script.Parent.Size = UDim2.new(math.clamp(stamina / maxStamina, 0, 1), 0, 1, 0)
end

userInputService.InputBegan:Connect(onInput)
userInputService.InputEnded:Connect(onInput)

runService.Heartbeat:Connect(function(deltaTime)
	if sprinting and not exhausted then
		stamina = math.max(0, stamina - drainRate * deltaTime)
		updateStaminaUI()
		humanoid.WalkSpeed = 24
		Tweenservice:Create(camera,TweenInfo.new(1,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{FieldOfView = 90}):Play()
		if stamina == 0 then
			sprint(false)
			exhausted = true
		end
	else
		Tweenservice:Create(camera,TweenInfo.new(1,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{FieldOfView = 70}):Play()
		updateStaminaUI()
		humanoid.WalkSpeed = 16
		if exhausted == true then
			sprint(false)
			wait(1)
			stamina = math.min(100, stamina + refreshRate * deltaTime)
			if stamina >= 10 then
				exhausted = false
			end	
		else
			stamina = math.min(100, stamina + refreshRate * deltaTime)
			exhausted = false
			if sprintHeld then
				sprint(true)
			end
		end
	end
end)

Where is this script stored in?

Also, could I see the layout of the workspace regarding where the script is stored?

What you need to do:
YOUR SCRIPT IS A LOCAL SCRIPT, THIS DOESN’T WORK. YOU CAN FIND LOCAL PLAYER BY HAVING THE SCRIPT LOCATED WITHIN STARTERPLAYERSCRIPTS AND REFERRING TO THE PLAYER AS script.Parent. YOU NEED TO CHANGE THE SCRIPT TO A SERVER SCRIPT
(also, for your inputs you coould make them seen by another local script which fires a remote event to this script.

Why:
I’ve heard of multiple instances of scripts only happening once. The solutions to these were because the script was inside of a local script, where in you changed the character’s walkspeed again, the server doesn’t know what’s happening and just resets the walkspeed back to the normal/ doesn’t change it.

StarterPlayerScripts should also be local, if I am not mistaken.

the sprint system works, its just a mechanic doesn’t work within the script

oh, what’s the mechanic you’re talking about?

thge exateustion mechanic within

A client-sided script does work for movement (although it is more convenient in some cases to move it elsewhere).

where should this be located in, for example?

odd. I looked at forum and it said it doesn’t wokr somewhere else. Script only working once and never again - #3 by Reditect

1 Like

also i gtg for around 2 hours.

I would probably put the movement script in StarterCharacterScripts.

No problem, I can try and solve it while you’re gone. Hopefully, others could also contribute as well.

snowd today so imma have fun, rarely snows here.

From the program you’ve given, it seems to work just fine?
Could you send a video of the issue in action, or clarify further upon the issue, whenever you get the chance?

as you can see, the exaustion mechanic starts, works, and then doesn’t.

That would be because of these lines:

if stamina >= 10 then
	exhausted = false
end	

As soon as the stamina reaches 10 again, it allows you to sprint right away.
You can modify the 10 to be a greater number if you want the player to wait till stamina reaches a greater value between sprinting.

the primary thing is that i want it to wait for a few seconds until it regenerates stamina

This would be hacked as it is a client only script.