Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
stepatron 103
5 years ago
Edited 5 years ago

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
0
Just so you know, the default walkspeed is 16. Your code (line 9) kicks a player for walking at the default speed. User#19524 175 — 5y
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 evaera 8028 — 5y
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. evaera 8028 — 5y
0
something like timeWaited = wait()? stepatron 103 — 5y

1 answer

Log in to vote
1
Answered by
evaera 8028 Trusted Badge of Merit Snack Break Game Jam Winner Moderation Voter Administrator Community Moderator Super Administrator
5 years ago

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)
0
Thank you so much :) stepatron 103 — 5y
Ad

Answer this question