How do i see player's velocity?

I’m trying to create an Anti-Cheat script that see whats the player’s Velocity, and i think that i can calculate the velocity using AssemblyLinearVelocity, but how can i do this? and i have another question, how can i make the script runs only when the player moves? i can use RunService and HeartBeat to run every frame but that can cause memory leak, right?

Thanks for reading! :hidere:

i would not rely on reading any velocity property from a character because even if an exploiter changes it it wouldn’t replicate to the server and you wouldn’t be able to detect it.

However what you can do is write down the players current position and after a second check it again and compare both of them. If the distance is really big it means the player has moved really fast.

Roblox has a simple tutorial on how to implement this.

To answer on your second question it’s not bad to have a heartbeat loop running and they do not cause a memory leak if you make sure to disconnect the event once you’re done with it.
You should just avoid doing big calculations in them because that could lead to performance issues.

i already tried to do that, but i didnt know if that is really precise, like if he change his WalkSpeed from 10 to 15, how can i put the magnitude exactly to see this difference?

You should make sure to change the WalkSpeed on the server and your code should use Humanoid.WalkSpeed to determin what the maximum allowed speed is.

I’ve written the code for it here.
If you need any help understanding something about the code feel free to ask.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		local humanoid = character.Humanoid
		local oldPosition = character:GetPivot().Position
		
		while character:IsDescendantOf(game) and humanoid.Health ~= 0 do
			local timeWaited = task.wait(.1)
			
			local newPosition = character:GetPivot().Position
			local speed = (oldPosition - newPosition).Magnitude
			local maxSpeed = humanoid.WalkSpeed * 1.4 * timeWaited --> multiply by 1.4 to leave some tolerance for latency
			
			if speed > maxSpeed then
				warn("too fast!")
				--character:PivotTo(CFrame.new(oldPosition)) --> teleport player back, this is what most games do
			else
				oldPosition = newPosition
			end
		end
	end)
end)
1 Like

it worked, thanks! but do you think that this could lag the game?

it’s just 1 loop for every player that runs every 0.1 seconds and all it does is compare the distance between 2 points. This shouldn’t be an issue.