Hey everyone!
Question, what is better to constantly check the character’s velocity?
Its for a client animation so its not important as antiExploit or related.
I was thinking on a RunService step, check (last position and current position).Magnitude ?
Just checking AssemblyLinearVelocity? or just Velocity property? Is any deprecated or not recommended?
Or even, Humanoid.Running(speed) ?
Just asking for suggestions and your recommended approaches.
It’s been a while since I’ve used any specific API, but taking the Velocity vector of e.g. the Torso and then deriving its magnitude should give you a fairly accurate reading. You can do this like so:
local player_speed = Vector3.new(player.Torso.Velocity.Magnitude.x, 0, player.Torso.Velocity.Magnitude.z);
Note how we omit the Y component of the velocity on purpose, because it means your speed could be skewed by an action such as the player jumping.
If this proves to be inaccurate for your work, you can do what you suggested and log the player’s position every X seconds. From past experience, I found that an interval of around every 0.2 seconds seems to work nicely.
It may look something like this:
RunService.RenderStepped:Connect(function()
local dt = 0.2;
local oldPosition = nil;
for i = 1, 2 do
if i == 1 then
oldPosition = primaryPartPos
elseif oldPosition then
local dp = (primaryPartPos - oldPosition)
dp = Vector3.new(dp.x, 0, dp.z);
local playerSpeed = dp.Magnitude/dt; --this is your answer!
end
if i == 1 then
task.wait(dt)
end
end
end)
This is obviously partly psuedo-code, so just take my answer and understand why/how it works and try to implement it yourself.
It’s best to go for the simplest solution :^)