Hello Devforum Browser!
I am making a game and one of the features requires me to detect if a player is walking. So I am just curious is it possible to detect player movement?
Thank You For Your Time!
Hello Devforum Browser!
I am making a game and one of the features requires me to detect if a player is walking. So I am just curious is it possible to detect player movement?
Thank You For Your Time!
Hi!
You can do this by checking the HumanoidStateType of the character’s humanoid…
This here shows you how to detect when it changes and when the player is walking: HumanoidStateType | Roblox Creator Documentation
Hope this helps
Im going to try this I will let you know if it works!
local prev, prevDelta
local avgSpeed = {}
function average(numbers)
local sum = 0
for i,v in pairs(numbers) do
sum = sum + v
end
return sum / #numbers
end
game:GetService("RunService").RenderStepped:Connect(function()
--check if character exists
if game.Players.LocalPlayer.Character then
if game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart") then
if prev then
--calculate speed, and average the last 10 measures
local newSpeed = (1 / (tick() - prevDelta)) * (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - prev).Magnitude
table.insert(avgSpeed,1,newSpeed)
if #avgSpeed > 10 then
table.remove(avgSpeed,#avgSpeed)
end
if math.floor(average(avgSpeed)) > 0 then
print("Walking")
end
end
--update the tracked position
prev = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
prevDelta = tick()
end
end
end)
I’m pretty sure you can use HumanoidStateType to detect if a players character is running, swimming, jumping, walking, etc.
local char = script.Parent
local hum = char:FindFirstChild("Humanoid")
if hum.StateType == Enum.HumanoidStateType.Walking or hum.StateType == Enum.HumanoidStateType.Running then
--code
end
Im pretty sure this works, correct me if im wrong.
Wow! Thanks for the code. I will test this out!
Literally just check if the Humanoid.MoveDirection magnitude isnt equal to 0