Detect if a Player is Moving or Not

I am looking to recreate my own version of an old zen/relaxation game from a long time ago. I want to make a script that when a person moves, it fires a command, and when the person stops moving it fires a different command. For demonstration purposes that command can be print(“moving”) and print(“not moving”).

Right now the first step I need to figure out is how to detect if a player is moving or not. I am still pretty new to scripting and want to find the most effective way of doing this.

I have looked through the Dev Forums, found a couple posts relating to this topic but none seem to work well. This script would most likely need to be on a local script because it will be per player

I attempted the script below which was ripped directly from the roblox developer API Reference, but even this script didnt seem to work.

game.Workspace.Player.Humanoid.Running:Connect(function(speed)
    if speed > 0 then
        print("Player is running")
    else
        print("Player has stopped")
    end
end)

I greatly appreciate all of your help and hope to create something very cool here soon!

8 Likes

You’re attempting to get the humanoid through the player instead of the character which is why it won’t work.
You need to define the character first before it comes to the humanoid of the character.

You can do something like this:
(Script is in StarterCharacterScripts btw)

local Character = script.Parent

Character.Humanoid.Running:Connect(function(speed)
	if speed > 0.1 then -- Walking
		print("Walking")
	else -- Idle
		print("Standing")
	end
end)
6 Likes

You can use the humanoids MoveDirection property and access its magnitude to see if they are walking (and it will only return a non zero value if the player is moving via inputs and not external forces like being flung by parts).

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			-- MoveDirection is a vector, specifically a unit vector to keep in mind
			-- and as a result of that, we can access its magnitude to get the
			-- 'speed' of that vector
			if hum.MoveDirection.Magnitude > 0 then
				print("player moving")
			else
				print("player not moving")
			end
		end)
	end)
end)
14 Likes

Hey, there’s a few ways you can do this, I personally would use “MoveDirection” to check if the player is moving or not, I’ve attached a link that can give you more information on “MoveDirection” if you’re interested.

If you would like to test my example, I just put a local script in “StarterPlayerScripts”.

Local Script:

local Player = game.Players.LocalPlayer --local player
repeat wait(1) until Player.Character --repeats wait(1) until the character has loaded
local Character = Player.Character --character

local Humanoid = Character:FindFirstChild("Humanoid") --finds the humanoid from the players character

local RunService = game:GetService("RunService") --run service

local function IsPlayerMoving() --function
	if Humanoid.MoveDirection.Magnitude == 0 then --if the movedirection is equal to 0 then the following will happen;
		print("Player is not moving") 
	elseif Humanoid.MoveDirection.Magnitude > 0 then --if the movedirection is greater than 0 then the following will happen;
		print("Player is moving")
	end
end

RunService.RenderStepped:Connect(IsPlayerMoving) --running the function

Hope this helped!

https://developer.roblox.com/en-us/api-reference/property/Humanoid/MoveDirection

33 Likes

This worked perfectly. The only thing I am having trouble trying to edit with this is the speed of the commands comming through. About every milisecond there is a command printing either the Player is moving or Player is not moving. I wanted to know if there is a way to make this print slower, possibly every second rather than every milisecond. Thank you for the help!

1 Like

Yeah, you can just add a debounce like this:

local Player = game.Players.LocalPlayer --local player
repeat wait(1) until Player.Character --repeats wait(1) until the character has loaded
local Character = Player.Character --character

local Humanoid = Character:FindFirstChild("Humanoid") --finds the humanoid from the players character

local RunService = game:GetService("RunService") --run service

local debounce = false
local timer = 1 --the amount of time

local function IsPlayerMoving() --function
	if debounce == false then
		debounce = true
		if Humanoid.MoveDirection.Magnitude == 0 then --if the movedirection is equal to 0 then the following will happen;
			print("Player is not moving") 
		elseif Humanoid.MoveDirection.Magnitude > 0 then --if the movedirection is greater than 0 then the following will happen;
			print("Player is moving")
		end
		wait(timer)
		debounce = false
	end
end

RunService.RenderStepped:Connect(IsPlayerMoving) --running the function
4 Likes

Hi what if i wanted for when the player is moving to switch animations to a different one, if we wait the time it wont work since it wont be instand and if we dont debounce the animation will just loop from the beginning.

I suggest you make a new post instead of bumping into this post

6 Likes