Can someone help me with this stamina script?


	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”)

Here is your code re-indented

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.

I see no problem with it stopping at print(“debug2”)

Elseif is meant to be if the variables do not meet the criteria of the conditions stated (aka If x is 56 then do this, else if x is 32 then do that)

If you are expecting a targeted outcome that is different from the current one, be specific and direct.

Can you show the entire code? You might be using UserInputService.InputBegan as what @ssurakkuu said. But you might be using ContextActionService, GuiButton.MouseButton1Click/GuiButton.Activated (for mobile buttons), or the deprecated Mouse.KeyDown instead.

We also don’t know if you’re using a Script or a LocalScript and if you’re using two or more scripts and a RemoteEvent.

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

The logic behind the code doesn’t make sense.

  • Is the player holding sprint and are they moving?
  • If yes:
  • 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
  1. Are we holding sprint? If yes:
  2. 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

You don’t need to apologize for your code. That’s what we’re here to help you with

@Puma_incredible Just bumping my reply above in case you didn’t saw it, i explained why it get stuck and gived you an eventual solution to solve it.

Thank you for the solution! it worked!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.