How would I check to see if my player is moving?

I’m trying to make my own project custom footstep sound system from scratch, and came across and issue. I’m trying to make a line of code that detects if the player is moving before it casts a ray below them to see what they are walking on, but the script keeps seeming to break. What way could I fix this?:

local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild(“Humanoid”)
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end

if character then
if character:FindFirstChild(“HumanoidRootPart”) then

  local part = character.HumanoidRootPart
  
  while true do
  	if humanoid.MoveDirection.Magnitude > 0 then --WHERE THE ISSUE MIGHT BE IDK
  		local origin = part.Position
  		local direction = part.CFrame.UpVector * -100

  		local raycastParams = RaycastParams.new()
  		raycastParams.FilterDescendantsInstances = {character}
  		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

  		local raycastResult = workspace:Raycast(origin, direction, raycastParams)

  	if raycastResult then
  		print(raycastResult.Position)
  		print(raycastResult.Material)
  		local sound = Instance.new("Sound")
  		sound.Parent = character.HumanoidRootPart
  		sound.Looped = false
  		sound.SoundId = "rbxassetid://300473653"
  		sound:Play()
  		wait(.5)
  		sound:Destroy()
  	end
  end

end
end
end

1 Like

Humanoid.Running (roblox.com)

You can check if the speed is 0 or above, and then play the sound if it is above 0.

local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild(“Humanoid”)
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end

if character then
if character:FindFirstChild(“HumanoidRootPart”) then

  local part = character.HumanoidRootPart

  while true do
  	humanoid.Running:Connect(function(speed)
  			local origin = part.Position
  			local direction = part.CFrame.UpVector * -100

  			local raycastParams = RaycastParams.new()
  			raycastParams.FilterDescendantsInstances = {character}
  			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

  			local raycastResult = workspace:Raycast(origin, direction, raycastParams)

  			if raycastResult then
  				print(raycastResult.Position)
  				print(raycastResult.Material)
  				local sound = Instance.new("Sound")
  				sound.Parent = character.HumanoidRootPart
  				sound.Looped = false
  				sound.SoundId = "rbxassetid://300473653"
  				sound:Play()
  				wait(.5)
  				sound:Destroy()
  			end
  	end)
  end

end
end

So something like this?? Sorry, I’m not really super advanced into scripting.

You can check if the humanoid’s movedirection or root part velocity magnitude is over 0.

You should first check the documentation, Humanoid haves a property called MoveDirection and can be used to check if the player is moving.

Sample script:

local RunService = game:GetService("RunService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local function CheckIfWalking()
    if Humanoid.MoveDirection.Magnitude > 0 then
        return true
    else
        return false
    end
end

RunService.RenderStepped:Connect(CheckIfWalking)
2 Likes