Detect Player's Movement

Hello, this script that I made is supposed to make it so every time the player moves it activates, however it doesn’t seem to count when the player jumps

local parent = script.Parent
local hum = script.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")

game:GetService("RunService").Stepped:Connect(function()

	local moveDirMag = hum.MoveDirection.Magnitude	
	if moveDirMag > 0 then

		print('moving')

	else

		print('stopped moving')

	end
end)

also is there a better way to make it so it only works whenever the player is moving rather than having this run all the time wasting resources?

and is there a better value to detect general movement/velocity etc?

3 Likes

you could try using Humanoid.Changed:Connect(

1 Like

Below detects if the player is jumping. Add this localscript to StarterCharacterScripts.
Also, I can’t answer your other questions you asked.

script.Parent.Humanoid.StateChanged:Connect(function(oldstate,newstate)
	if newstate == Enum.HumanoidStateType.Jumping then
		print("Player is jumping.")
	end
end)
1 Like

the problem with this is that it doesn’t count if for example the player was sent midair and rather if it manually jumped, yknow?

this works for the second part thank you.
hum.Changed:Connect(function()

Yes.

New code:

--//Servives
local RunService = game:GetService("RunService")

--//Variables
local parent = script.Parent
local hum = parent.Parent.Parent.Parent:WaitForChild("Humanoid")

--//Tables
local Connections = {}

--//Functions
hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	local connection = Connections.Stepped

	if connection then
		connection:Disconnect()
		Connections.Stepped = nil
	end
	
	if hum.MoveDirection.Magnitude <= 0 then
		print("stopped moving")
		
		return
	end
	
	Connections.Stepped = RunService.Stepped:Connect(function()
		print('moving')
	end)
end)

If you can tell me your usecase, it would help making this more efficient.

2 Likes

Edited the code a bit to make it more accurate.

1 Like

sure thing, I’m trying to make this particle that is in the player’s head activate every time the player is moving , but not only when its ordered to and rather just moving in general, pushed, sent flying, etc
i’ve tried detecting if the current floormaterial is air but just haven’t been able to properly implement it sadly

local hum = script.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
local light = parent:FindFirstChildOfClass('PointLight')

hum.Changed:Connect(function()

	local moveDirMag = hum.MoveDirection.Magnitude
	local material = hum:GetPropertyChangedSignal("FloorMaterial")
	local jumped = hum:GetPropertyChangedSignal('Jump')
	
	if moveDirMag > 0 then
		
		for i,att in pairs (parent:GetChildren()) do
			if att:IsA('Attachment') then
				for i,pet in pairs (att:GetChildren()) do
					if pet:IsA('ParticleEmitter') then

						pet.Enabled = true
					end
				end
			end
		end

		for i,part in pairs (parent:GetChildren()) do
			if part:IsA('Part') then
				for i,pet in pairs (part:GetChildren()) do
					if pet:IsA('ParticleEmitter') then

						pet.Enabled = true

					end
				end
			end
		end

	else

		for i,att in pairs (parent:GetChildren()) do
			if att:IsA('Attachment') then
				for i,pet in pairs (att:GetChildren()) do
					if pet:IsA('ParticleEmitter') then

						pet.Enabled = false
					end
				end
			end
		end

		for i,part in pairs (parent:GetChildren()) do
			if part:IsA('Part') then
				for i,pet in pairs (part:GetChildren()) do
					if pet:IsA('ParticleEmitter') then

						pet.Enabled = false

					end
				end
			end
		end
	end
end)

here’s my messy code

Try this:

--//Services
local parent = script.Parent
local hum = parent.Parent.Parent.Parent:WaitForChild("Humanoid")
local light = parent:FindFirstChildOfClass('PointLight')

--//Functions
local function SetVisuals(bool)
	for i, child in ipairs (parent:GetChildren()) do
		if not child:IsA("Attachment") and not child:IsA("Part") then
			continue
		end
		
		for i, pet in ipairs(child:GetChildren()) do
			if pet:IsA("ParticleEmitter") then
				pet.Enabled = bool
			end
		end
	end
end

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		print("Player in air")
		
		SetVisuals(true)
		
		return
	end
	
	local moveDirMag = hum.MoveDirection.Magnitude

	if moveDirMag > 0 then
		SetVisuals(true)
	else
		SetVisuals(false)
	end
end)
1 Like

the jumping part only seems to work when the player moves and jumps rather than when its just midair

Okay, I added a new function to detect when the FloorMaterial has changed.

--//Services
local parent = script.Parent
local hum = parent.Parent.Parent.Parent:WaitForChild("Humanoid")
local light = parent:FindFirstChildOfClass('PointLight')

--//Functions
local function SetVisuals(bool)
	for i, child in ipairs (parent:GetChildren()) do
		if not child:IsA("Attachment") and not child:IsA("Part") then
			continue
		end

		for i, pet in ipairs(child:GetChildren()) do
			if pet:IsA("ParticleEmitter") then
				pet.Enabled = bool
			end
		end
	end
end

hum:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		print("Player in air")

		SetVisuals(true)
	end
end)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		return
	end

	local moveDirMag = hum.MoveDirection.Magnitude

	if moveDirMag > 0 then
		SetVisuals(true)
	else
		SetVisuals(false)
	end
end)
1 Like

this works almost perfectly!
one last issue is that it doesn’t turn off when the player is idle on the floor
( this only seems to be activated when the player moves )

so basically its not detecting that the player stopped moving

Oops, forgot to account for that.

New code:

--//Services
local parent = script.Parent
local hum = parent.Parent.Parent.Parent:WaitForChild("Humanoid")
local light = parent:FindFirstChildOfClass('PointLight')

--//Functions
local function SetVisuals(bool)
	for i, child in ipairs (parent:GetChildren()) do
		if not child:IsA("Attachment") and not child:IsA("Part") then
			continue
		end

		for i, pet in ipairs(child:GetChildren()) do
			if pet:IsA("ParticleEmitter") then
				pet.Enabled = bool
			end
		end
	end
end

hum:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		print("Player in air")

		SetVisuals(true)
	elseif hum.MoveDirection.Magnitude <= 0 then
		SetVisuals(false)
	end
end)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		return
	end

	local moveDirMag = hum.MoveDirection.Magnitude

	if moveDirMag > 0 then
		SetVisuals(true)
	else
		SetVisuals(false)
	end
end)

Edit: Added a check in the FloorMaterial function to disable the visuals if the player isn’t moving.

1 Like

works perfectly thank you so much!

1 Like

No problem. If you have any more questions, feel free to ask.

1 Like

yo, having a bit of an issue, the code seems to work fine on the editor but just doesnt work when jumping when playing on a normal game

--//Services
local parent = script.Parent
local hum = parent.Parent.Parent.Parent.Humanoid

--//Functions
local function SetVisuals(bool)
	local model = parent.plh_trail

	for i, part in ipairs(model:GetChildren()) do
		if part:IsA("Attachment") or part:IsA("Part") then

			for i, pet in ipairs(part:GetChildren()) do
				if pet:IsA("ParticleEmitter") then

					pet.Enabled = bool

				end
			end
		end
	end
end


hum:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		SetVisuals(true)
	elseif hum.MoveDirection.Magnitude <= 0 then
		SetVisuals(false)
	end
end)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		return
	end

	local moveDirMag = hum.MoveDirection.Magnitude

	if moveDirMag > 0 then
		SetVisuals(true)
	else
		SetVisuals(false)
	end
end)

im currently using this, should be working the same but slightly accommodated to my game

here it is so you can verify if you want to
will reply whenever i wake up so no rush

Try this:

--//Variables
local parent = script.Parent
local hum = parent.Parent.Parent.Parent.Humanoid
local model = parent.plh_trail

--//Functions
local function SetVisuals(bool)
	for i, child in ipairs(model:GetDescendants()) do
		if not child:IsA("Attachment") and not child:IsA("Part") then
			continue
		end

		for i, pet in ipairs(child:GetChildren()) do
			if pet:IsA("ParticleEmitter") then
				pet.Enabled = bool
			end
		end
	end
end

hum:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		SetVisuals(true)
	elseif hum.MoveDirection.Magnitude <= 0 then
		SetVisuals(false)
	end
end)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		return
	end

	local moveDirMag = hum.MoveDirection.Magnitude

	if moveDirMag > 0 then
		SetVisuals(true)
	else
		SetVisuals(false)
	end
end)
1 Like

oh im so dumb sorry, i had it set to a server script instead of local, idk if that matters but… it fixed my issue?, thank you again tho lol!

very slightly edited code that fits my requirements rn:

--//Services
local parent = script.Parent
local hum = parent.Parent.Parent.Parent.Humanoid
local model = parent.plh_trail

--//Functions
local function SetVisuals(bool)
	for i, child in ipairs (model:GetDescendants()) do
		if not child:IsA("Attachment") and not child:IsA("Part") then
			continue
		end

		for i, pet in ipairs(child:GetChildren()) do
			if pet:IsA("ParticleEmitter") then
				pet.Enabled = bool
			end
		end
	end
end

hum:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		SetVisuals(true)
	elseif hum.MoveDirection.Magnitude <= 0 then
		SetVisuals(false)
	end
end)

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if hum.FloorMaterial == Enum.Material.Air then
		return
	end

	local moveDirMag = hum.MoveDirection.Magnitude

	if moveDirMag > 0 then
		SetVisuals(true)
	else
		SetVisuals(false)
	end
end)
1 Like