#I need help for a sprinting script

12 messages · Page 1 of 1 (latest)

charred lagoon
#

So I have this sprint script, but I want to make it so when you are standing still while pressing down on the sprint button (LeftShift) it wont take away any stamina, right now when you sprint while staying still, the stamina keeps going away.
Here's the script to edit if you can help;

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playergui = player.PlayerGui

local stamina = 300
local maxstamina = 300
local speeddifference = 12
local drainrate = 20
local refreshrate = 10
local staminarefresh = 60

local sprintheld = false
local sprinting = false
local exhausted = false

local function sprint(active)
if exhausted then return end

humanoid.WalkSpeed = active and humanoid.WalkSpeed + speeddifference or humanoid.WalkSpeed - speeddifference
sprinting = active
end

local function onInput(input)
if input.KeyCode == Enum.KeyCode.LeftShift and input.UserInputType ~= Enum.UserInputType.Gamepad1 then
sprintheld = input.UserInputState == Enum.UserInputState.Begin
sprint(sprintheld)
end
end

local function updateStaminaUI()
playergui.StaminaGUI.StaminaFrame.StaminaBar.Size = UDim2.new(math.clamp(stamina/maxstamina, 0, 1), 0, 1, 0)
end

uis.InputBegan:Connect(onInput)

uis.InputEnded:Connect(onInput)

rs.Heartbeat:Connect(function(deltaTime)
if sprinting then
stamina = math.max(0, stamina - drainrate * deltaTime)
updateStaminaUI()
print(math.floor(stamina))
if stamina == 0 then
sprint(false)
exhausted = true
end
else
stamina = math.min(maxstamina, stamina + refreshrate * deltaTime)
if stamina >= staminarefresh then
updateStaminaUI()
exhausted = false
print(math.floor(stamina))
if sprintheld then
sprint(true)
end
end
end
end)

rs.Heartbeat:Connect(updateStaminaUI)

muted epoch
#

make it so both shift and W

#

Will activate the stamina

charred lagoon
#

ok thanks

vocal kiln
muted epoch
charred lagoon
#

yeah it works pretty well

#

had a dev on my game check it out too

#

no problems on it

muted epoch
#

yea its and easy fix

#

so that way you would have to be sprinting to run out of stamina