hey there, i have a sprinting script that works fine, i also have a “Jogging” thing which basically should work the same as sprinting, but uses less stamina, the only problem is my solution (staminaCost2) doesnt work:
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
repeat wait() until players.LocalPlayer.Character
local character = players.LocalPlayer.Character
local maxStamina = 500
local staminaRegen = 1
local staminaCost = 2
local staminaCost2 = .8
local sprintSpeed = 22
local jogSpeed = 13
local walkSpeed = 6
local currentStamina = maxStamina
local FullSprintAnim = character.Humanoid:LoadAnimation(script.Animation)
local WalkAnim = character.Humanoid:LoadAnimation(script.Walk)
local anim = character:WaitForChild("Animate")
character.Humanoid.WalkSpeed = 7
anim.walk.WalkAnim.AnimationId ="rbxassetid://6648900668"
-- Update Stamina GUI
function updateGui(current, max)
if character.Humanoid.Health <= 0 then
players.LocalPlayer.PlayerGui.Stamina.Enabled = false
end
players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(((current/max)*0.3)-0.3, 297, 0, 50)
end
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
FullSprintAnim:Play()
character.Humanoid.WalkSpeed = sprintSpeed --connects local sprint speed
end
end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
FullSprintAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
end
end)
runService.Heartbeat:Connect(function()
if character.Humanoid.WalkSpeed == sprintSpeed then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost
else
character.Humanoid.WalkSpeed = walkSpeed
FullSprintAnim:Stop()
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
end
end
updateGui(currentStamina, maxStamina)
end)
------------------------------------------------------------------------- X
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.X then
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
FullSprintAnim:Play()
character.Humanoid.WalkSpeed = jogSpeed
end
end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.X then
FullSprintAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
end
end)
runService.Heartbeat:Connect(function()
if character.Humanoid.WalkSpeed == sprintSpeed then
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost2
else
character.Humanoid.WalkSpeed = walkSpeed
FullSprintAnim:Stop()
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
end
end
updateGui(currentStamina, maxStamina)
end)
You can’t have two functions bound to the same event. You can only have one InputBegan, InputEnded, and Heartbeat. You need to use elseif in those to allow an event to do multiple things.
if input.KeyCode == Enum.KeyCode.LeftShift then
do stuff
elseif input.KeyCode == Enum.KeyCode.X then
do stuff
end
these are the changes i have made, now its randomly sprinting automatically, stamina wont regen, and im confused now:
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
repeat wait() until players.LocalPlayer.Character
local character = players.LocalPlayer.Character
local maxStamina = 500
local staminaRegen = 1
local staminaCost = 2
local staminaCost2 = .8
local sprintSpeed = 22
local jogSpeed = 13
local walkSpeed = 6
local currentStamina = maxStamina
local FullSprintAnim = character.Humanoid:LoadAnimation(script.Animation)
local WalkAnim = character.Humanoid:LoadAnimation(script.Walk)
local LightJogAnim = character.Humanoid:LoadAnimation(script.LightJog)
local anim = character:WaitForChild("Animate")
character.Humanoid.WalkSpeed = 7
anim.walk.WalkAnim.AnimationId ="rbxassetid://6648900668"
-- Update Stamina GUI
function updateGui(current, max)
if character.Humanoid.Health <= 0 then
players.LocalPlayer.PlayerGui.Stamina.Enabled = false
end
players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(((current/max)*0.3)-0.3, 297, 0, 50)
end
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
end
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
FullSprintAnim:Play()
character.Humanoid.WalkSpeed = sprintSpeed --connects local sprint speed
elseif input.KeyCode == Enum.KeyCode.X then
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
LightJogAnim:Play()
character.Humanoid.WalkSpeed = jogSpeed --connects local jog speed
end
end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
FullSprintAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
elseif input.KeyCode == Enum.KeyCode.X then
LightJogAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
end
end)
runService.Heartbeat:Connect(function()
if character.Humanoid.WalkSpeed == sprintSpeed then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost
elseif character.Humanoid.WalkSpeed == jogSpeed then
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost2
else
character.Humanoid.WalkSpeed = walkSpeed
FullSprintAnim:Stop()
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
end
end
end
updateGui(currentStamina, maxStamina)
end)
I need sleep. If you don’t get it resolved by tomorrow feel free to DM me and I will show you when I get off work. You should start with the elseifs, they’re in the wrong spots. I’d show you now, but I’m on mobile. Good luck.
Your problem is the way the ifs and elseifs are lined up. Fix the indentation first.
if
indented stuff
indented if
double indented stuff
indented elseif
double indented stuff
indented end
elseif
indented stuff
end
Here is the correctly indented code with a little summary of what is going only
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
-- Does nothing currently
end
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then -- This runs whenever any key is pressed if you have stanima and are moving.
FullSprintAnim:Play()
character.Humanoid.WalkSpeed = sprintSpeed
elseif input.KeyCode == Enum.KeyCode.X then -- Because this is an elseif, this runs only if you don't have enough stamina or you aren't moving, AND you press X.
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
LightJogAnim:Play()
character.Humanoid.WalkSpeed = jogSpeed
end
end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then -- This is correct, actually.
FullSprintAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
elseif input.KeyCode == Enum.KeyCode.X then -- Still correct.
LightJogAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
end
end)
runService.Heartbeat:Connect(function()
if character.Humanoid.WalkSpeed == sprintSpeed then -- If you are sprinting
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then -- If you are sprinting and have enough stamina and are moving
currentStamina = currentStamina - staminaCost
elseif character.Humanoid.WalkSpeed == jogSpeed then -- If you are sprinting and you are jogging at the same time somehow
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then -- If you are sprinting and jogging and have enough stamina and are moving
currentStamina = currentStamina - staminaCost2
else
character.Humanoid.WalkSpeed = walkSpeed -- If you are sprinting and jogging but either not moving or don't have enough stamina.
FullSprintAnim:Stop()
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
end
end
end
updateGui(currentStamina, maxStamina)
end)
And here is the code as it should be.
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
-- I deleted an end here
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
FullSprintAnim:Play()
character.Humanoid.WalkSpeed = sprintSpeed
end -- I put an end here
elseif input.KeyCode == Enum.KeyCode.X then
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
LightJogAnim:Play()
character.Humanoid.WalkSpeed = jogSpeed
end
end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
FullSprintAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
elseif input.KeyCode == Enum.KeyCode.X then
LightJogAnim:Stop()
character.Humanoid.WalkSpeed = walkSpeed
end
end)
runService.Heartbeat:Connect(function()
if character.Humanoid.WalkSpeed == sprintSpeed then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost
end -- I put an end here
elseif character.Humanoid.WalkSpeed == jogSpeed then
if currentStamina >= staminaCost2 and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost2
else
character.Humanoid.WalkSpeed = walkSpeed
FullSprintAnim:Stop()
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
-- I deleted an end here
end
end
updateGui(currentStamina, maxStamina)
end)
You see how the ifs and elseifs that are related to the same things all line up? Checking the walkspeed or keycode falls on a first indentation if or elseif, checking the current stamina falls on a second indentation if or else if. Let me know if you have any other issues with it.
that works, thanks, but while your sprinting or jogging, if you keep holding down the key (Left Shift or X) the stamina bar will stop and the animation will keep playing if you stop moving but keep holding down the key.
I’m not sure if I follow 100%, but it sounds like you need to check if they are moving in your heartbeat function.
runService.Heartbeat:Connect(function()
if character.Humanoid.WalkSpeed == sprintSpeed and character.Humanoid.MoveDirection.Magnitude > 0 then
...
...
elseif character.Humanoid.WalkSpeed == jogSpeed and character.Humanoid.MoveDirection.Magnitude > 0 then
that fixes the stamina not regenerating, but the animation is still playing even after the player stops moving, but is still holding down shift or x (depending on what they are doing; running or jogging)