The player walking detection script does not work

Here’s my script.

while power > 0 and Character.Humanoid.MoveDirection.Magnitude > 0 do
						power = power - .02
						script.Parent.Parent.Parent.ScreenGui.stamina.gauge.Size = UDim2.new(power / 10, 0, 0.674, 0)
						--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
						wait()
						if power <= 0 then
							Character.Humanoid.WalkSpeed = 16
							running = false
							PlayAnim:Stop()	
						end
					end


My script is in startergui
i Creating a script that reduces the gauge when walking.
so this means that this script does not work.
But I can’t detect walking

How can I solve this problem?

If you need any information, I will provide you with more.

1 Like

Try formatting it this way:

while power > 0 do
	if Character.Humanoid.MoveDirection.Magnitude > 0 then
	   -- do stuff
	end
	wait()
end

I think I know what’s happening here. You are using a while loop which check for the power. As soon as the power is less then 0 it stops, problem is that the if statement also checking for the power and responsible for changing the humanoids walkspeed, can’t run as the while loop stops before.

Does this work?

while wait() do
    if Character.Humanoid.MoveDirection.Magnitude >= 0 then return end
    power = power - .02
    script.Parent.Parent.Parent.ScreenGui.stamina.gauge.Size = UDim2.new(power / 10, 0, 0.674, 0)
    --Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
    if power <= 0 then
        Character.Humanoid.WalkSpeed = 16
        running = false
        PlayAnim:Stop()
        return	
    end
end

Does this fix your issue?

Thank you both, but I’m facing a new problem, so it’ll probably take a long time… :anguished:

What issue do you have?
char limit

I don’t know if this will help with your new problem but if your script is in starter gui, how are you able to do character.Humanoid? wouldn’t you have to do character:WaitForChild(“Humanoid”)?

local Player = game.Players.LocalPlayer

local Character = game.Workspace:WaitForChild(Player.Name)

local Anim = Instance.new('Animation', Character.Humanoid)
Anim.AnimationId = 'rbxassetid://15710495400'

local function sprint()
	local p = Character.Humanoid:LoadAnimation(Character.Humanoid.Animation)
	if sprinting then
		p:Play()
		sprinting = false
	else
		PlayAnim:Stop()		
		sprinting = true
	
	end
end
button.MouseButton1Click:Connect(sprint)

The animation dosen’t stop!

Problem solved! Now let’s try the any solution!

Do you still need help with this? If yes, where do you get the sprinting variable and what is the “PlayAnim” animation?