if Sprinttoggled and Humanoid.MoveDirection.Magnitude > 0 then
print("debug1")
if Energyboost.Value == false then
stamina = stamina - 1.3
print("debug2")
elseif Sprinttoggled.Value == false and Humanoid.MoveDirection.Magnitude == 0 then
print("debug3")
if Energyboost.Value == false then
stamina = stamina + 0.7
print("debug4")
end
end
end
Can someone help me with this script the code stops at print(“debug2”)
if Sprinttoggled and Humanoid.MoveDirection.Magnitude > 0 then
print("debug1")
if Energyboost.Value == false then
stamina = stamina - 1.3
print("debug2")
elseif Sprinttoggled.Value == false and Humanoid.MoveDirection.Magnitude == 0 then
print("debug3")
if Energyboost.Value == false then
stamina = stamina + 0.7
print("debug4")
end
end
end
The issue you are most likely experiencing is if you are only running this if statement checking sprinttoggled only when the player is pressing shift, you would need to detect when the player is letting go of shift too, i’d recommend using UserInputService and run a function to determine if they should be sprinting or not.
local Input = game:GetService("UserInputService")
local InputMOBILE = game:GetService("TouchInputService")
local bar = script.Parent
local stamina = 100
local Tween = game:GetService("TweenService")
local Sprinttoggled = false
local Players = game.Players.LocalPlayer
local character = Players.Character
local Humanoid = character:WaitForChild("Humanoid")
local Key = Enum.KeyCode.LeftShift
local Energyboost = Players:WaitForChild("Energyboost")
local ContextActionService = game:GetService("ContextActionService")
local function sprinttoggle(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print(actionName, inputObject)
if Sprinttoggled == false then
Sprinttoggled = true
elseif Sprinttoggled == true then
Sprinttoggled = false
end
end
end
ContextActionService:BindAction("Sprinttoggled", sprinttoggle, true)
ContextActionService:SetImage("Sprinttoggled", "rbxassetid://14722754437")
Input.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.ButtonL2 then
if Sprinttoggled == false then
Sprinttoggled = true
end
end
end)
Input.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.ButtonL2 then
if Sprinttoggled == true then
Sprinttoggled = false
end
end
end)
Input.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
if Sprinttoggled == false then
Sprinttoggled = true
end
end
end)
Input.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
if Sprinttoggled == true then
Sprinttoggled = false
end
end
end)
while wait() do
if Sprinttoggled == true then
Humanoid.WalkSpeed = 21
else
Humanoid.WalkSpeed = 16
end
if stamina == 0 then
Humanoid.WalkSpeed = 16
Sprinttoggled = false
end
if Sprinttoggled and Humanoid.MoveDirection.Magnitude > 0 then
print("debug1")
if Energyboost.Value == false then
stamina = stamina - 1.3
print("debug2")
elseif Sprinttoggled == false and Humanoid.MoveDirection.Magnitude == 0 then
print("debug3")
if Energyboost.Value == false then
stamina = stamina + 0.7
print("debug4")
end
end
end
stamina = math.clamp(stamina, 0, 100)
bar:TweenSize(UDim2.new((1/100) * stamina, 0, 1 ,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0)
end
Here is the full script
This is in a local script by the way
The code stop at debug2 because the checks are inconsistent.
At the very top, you check if Sprinttoggled is true before to run the code:
if Sprinttoggled and Humanoid.MoveDirection.Magnitude > 0 then
-- Everything here is running only if Sprinttoggled.Value = true and the character is moving
end
Then all the code inside this (if-then) check can be runned only if Sprinttoggled is true.
And here come the problem, after the debug2 print, you’re doing another check that make sure Sprinttoggled is false to run the code, but if Sprinttoggled is actually false, the code itself in here isn’t going to be runned as you previously made a check to run the code only if this value is true.
if Sprinttoggled and Humanoid.MoveDirection.Magnitude > 0 then
-- V It will never work because if it's false, eveyrhing here isn't going to run
if Sprinttoggled.Value == false then
--Do something...
end
end
So the correct code would be:
if Sprinttoggled.Value == true and Humanoid.MoveDirection.Magnitude > 0 and Energyboost.Value == false then
stamina -= 1.3
elseif Sprinttoggled.Value == false and Humanoid.MoveDirection.Magnitude == 0 and Energyboost.Value == false then
stamina += 0.7
end
no energyboost? then decrease stamina and (debug2)
elseif (so yes energyboost), and not holding sprint and not moving???
this will never happen, because you’re doing
Are we holding sprint? If yes:
Are we not holding sprint? If yes:
if condition then -- this checks if condition is met
print("true!")
else -- This happens if the initial condition is not met
print("false")
end
if condition then
print("true!")
elseif othercondition then -- This happens if the initial condition is not met AND another condition is met. It's the combination of an else and an if, therefore elseif
print("other true!")
end
-- It's the same as:
if condition then
print("true!")
else
if othercondition then
print("other condition is true!")
end
end
Sorry that my code’s logic does not make so much sense this is the first time I made a script mostly by myself with the only line of code taken from youtube is the one that makes the sprint bar tween