How to detect if character is moving

I am trying to make a pretty simple shift to sprint script, however there is one small issue, When you are pressing shift but not moving your stamina still goes down, I want it to stop using stamina when you aren’t moving (if you are still holding shift I want the effects applied when you start moving)

local player = game.Players.LocalPlayer

local char = player.Character



local UIS = game:GetService("UserInputService")



local stamina = script.Value.Value

local isRunning = false



stamina = math.clamp(stamina, 0, 250)



UIS.InputBegan:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.LeftShift then

		isRunning = true

		char.Humanoid.WalkSpeed = 20



		while stamina > 0 and isRunning do

			stamina = stamina -1

			script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)

			wait()

			if stamina <= 0 then

				char.Humanoid.WalkSpeed = 12

			end



		end

	end

end)



UIS.InputEnded:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.LeftShift then

		isRunning = false

		char.Humanoid.WalkSpeed = 12



		while stamina < 250 and not isRunning do

			stamina = stamina +0

			script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)

			wait()

			if stamina <= 0 then

				char.Humanoid.WalkSpeed = 12

			end



		end

	end

end)

(formatting messed up a bit while pasting)

1 Like

Here’s a few ways you can detect movement stopping:

Humanoid.MoveDirection = Vector3.zero

--or

Humanoid:GetState()
1 Like

You can also check if the humanoid magnitude is greater than zero :

if Humanoid.MoveDirection.Magnitude > 0 then
1 Like

im not quite the best scripter, but i tried to put it in my code like this

local getstate = Humanoid:GetState() --also tried GetState(Vector3)
--rest of the code
while stamina > 0 and isRunning do
			if getstate > 0 then

			stamina = stamina -1

and get the error attempt to compare number < EnumItem
you know any way to fix this?

1 Like

Just check like this

local runservice = game:GetService("RunService") -- Looping but using frames (fps)

runservice.Hearbeat:Connect(function()
	if Humanoid.MoveDirection == Vector3.new() then -- checks if the movedirection = Vector3.new(0,0,)
		--print("idle")
	else
		--print("running")
	end
end)
2 Likes

This works quite well, but it does hurt my framerate a bit, and my stamina bar depletes in like half a second now, i think this might be because of the loop

1 Like

:GetState tells you what the HumanoidState is, which is not a number.
I advise doing what @FireStrykerAzul suggested, and use the MoveDirection’s Magnitude property.

if Humanoid.MoveDirection.Magnitude > 0 then -- they are moving
    -- do stuff here
end
1 Like

Heartbeat gives an argument called step which describes how long it was since the last heartbeat event. You can write something like Stamina -= DepletionRate * Step which will remove DepletionRate every second

1 Like

That solution works great, I tried it before but it would freeze my studio, but then i check my code again and i accidentally put it in a while loop, and it was eating all the resources trying to check the magnitude that fast in the loop

1 Like

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