Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I get the distance a player walked every every frame? For anti teleport script.

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

4 comments

0

Just so you know, the default walkspeed is 16. Your code (line 9) kicks a player for walking at the default speed.

2

Be careful with code like this. Your code does not account for server throttling. The wait function does not always wait the amount of time you give it. If your game has a lot of players all doing things and walking around, it's possible that it will be significantly longer (up to 10 seconds or even more until your script resumes from the yield). If that were to happen, you'd have kicked any

1

...players who moved during the time that your server throttled. wait returns a number, which is the actual amount of time waited. You should consider this as a factor in your process of determining if the player teleported.

0

something like timeWaited = wait()?

1 answer

✓ Accepted answer

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)

1 comment

0

Thank you so much :)

This discussion is preserved as it was. New answers, comments, and votes are closed.