Hi there! I'm curious on how to get the distance a player walked, the one I have right now is pretty bad since it doesn't get how many studs a player walked so its almost exactly the walkspeed of the player. Can anyone help?
if checkState(player, "antiTeleport", humanoid:GetState()) then --Just to check if the player is NOT jumping/falling local humanoidRootPart = player.Character:WaitForChild("HumanoidRootPart") local playerPosition = humanoidRootPart.Position wait() local currentPosition = humanoidRootPart.Position print((currentPosition - playerPosition).magnitude) --Just for debug if (currentPosition - playerPosition).magnitude > 15 then wait(1/10) if not isBanned(player) then dataStore:SetAsync(key, {true, 168, os.time()}) --Just to ban player:Kick(reason) print(player.Name .. " has been banned!") return end end end
Here's a demo I just threw together for you. Since WalkSpeed is a number in studs/second, all we need to do to find the speed is divide the difference between the previous position and the current position by the time waited to get us the player's speed in terms of WalkSpeed.
This implementation actually works no matter how often you check, so you could change the wait(1)
to wait(0.2)
, but there's no real reason to be running this code that often because excess code running every Lua cycle is what causes servers to throttle in the first place.
function HookCharacter(character) local root = character:WaitForChild("Humanoid").RootPart local lastPosition = root.Position while true do local timeWaited = wait(1) local position = root.Position local speed = (lastPosition - position).magnitude / timeWaited print(("%s's speed: %f"):format(character.Name, speed)) -- Compare speed here, `if speed > 20 then`.... lastPosition = position end end function HookPlayer(player) player.CharacterAdded:Connect(HookCharacter) if player.Character then HookCharacter(player.Character) end end game.Players.PlayerAdded:Connect(HookPlayer)