So I've this sprint script that just makes a character sprint. However, sometimes it drains twice as much stamina as it should. I'm pretty sure it's because the two ways the sprint function can be fired fire at the same time. How do I prevent this?
Here's the script (LocalScript in a ScreenGui in the StarterGui)
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(.15,Enum.EasingStyle.Linear)
local rs = game:GetService("ReplicatedStorage")
local stamina = 100
local draining = false
local player = game.Players.LocalPlayer
local character = player.Character
if character == nil then
player.CharacterAdded:Wait()
character = player.Character
end
local sprinting = false
local anim = rs:WaitForChild("Sprint")
local sprintanim = character:WaitForChild("Humanoid"):LoadAnimation(anim)
local staminagui = player:WaitForChild("PlayerGui"):WaitForChild("StaminaGui"):WaitForChild("Frame")
local function stopsprint()
if sprinting == true then
sprinting = false
local tween = ts:Create(game.Workspace.CurrentCamera, ti, {FieldOfView = 70})
tween:Play()
local tween2 = ts:Create(character.Humanoid, ti, {WalkSpeed = 16})
tween2:Play()
sprintanim:Stop()
repeat
if stamina < 100 then
stamina = stamina + 1
staminagui.StamNum.Text = stamina .. " / 100"
if stamina > 25 then
staminagui:WaitForChild("StamNum").TextColor3 = Color3.new(0,0,0)
character.Humanoid.JumpPower = 50
end
if stamina <= 25 then
staminagui.StamNum.TextColor3 = Color3.new(255/255, 0/255, 0/255)
character.Humanoid.JumpPower = 0
end
end
wait(0.1)
until
sprinting == true
end
end
local function sprint()
if sprinting == false and character.Humanoid.PlatformStand == false and stamina > 25 then
sprinting = true
local tween = ts:Create(game.Workspace.CurrentCamera, ti, {FieldOfView = 90})
tween:Play()
sprintanim:Play()
local tween2 = ts:Create(character.Humanoid, ti, {WalkSpeed = 24})
tween2:Play()
repeat
draining = true
stamina = stamina - 1
staminagui.StamNum.Text = stamina .. " / 100"
if stamina > 25 then
staminagui:WaitForChild("StamNum").TextColor3 = Color3.new(0,0,0)
character.Humanoid.JumpPower = 50
end
if stamina <= 25 then
staminagui.StamNum.TextColor3 = Color3.new(255/255, 0/255, 0/255)
character.Humanoid.JumpPower = 0
end
if stamina == 0 then
stopsprint()
end
wait(0.1)
draining = false
until
sprinting == false
end
end
local moving = false
local hittingshift = false
character:WaitForChild("Humanoid").Running:Connect(function(speed)
if speed > 0 then
moving = true
if hittingshift == true then
sprint()
end
else
moving = false
stopsprint()
end
end)
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
hittingshift = true
if moving == true then
sprint()
end
end
end)
game:GetService("UserInputService").InputEnded:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
hittingshift = false
stopsprint()
end
end)

4 comments
The thing is, when the sprint() function is removed from either places where it is, either hitting shift while standing still, then moving does nothing, or hitting shift while moving does nothing.
Well, try adding prints and also prints for boolean. And tell what it prints.
Nevermind, I got it to work
It will be helpful for future users with identical problem, if you edit to what you done to fix your isue.