How do I make it so that my script get's the player's average speed (studs) from point A to point B?

script.Parent = nil and script.Parent.Parent == nil

-- Variables

local maxDistance = 22

local invisibleWall = workspace.Map:WaitForChild("Invisible Wall")

local spawnLocation = workspace.Map:WaitForChild("Spawn").SpawnLocation

local textonKick  = "Your exploit is garbage"

game:GetService("Players").PlayerAdded:Connect(function(player)
    
    player.CharacterAdded:Connect(function(Character)
            
            -- Player distance travelled detection
            
            wait(4)
            
            local LastPosition = nil        
            local function RepeatEverySecond()
    
            if LastPosition then
            local DistanceTravelled = (Character.HumanoidRootPart.Position - LastPosition).Magnitude 
            if DistanceTravelled >  maxDistance then
            player.Character:FindFirstChild("HumanoidRootPart").CFrame = spawnLocation.CFrame + Vector3.new(0,7,0)
            end
        end
            LastPosition = Character.HumanoidRootPart.Position
        end
        invisibleWall.Touched:Connect(function(hit)
        hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = spawnLocation.CFrame + Vector3.new(0,7,0) -- If the wall is touched, bring the player back to spawn    
            end)

            while true do 
            RepeatEverySecond()
            wait(1)

        end
        
      end)
end)

Please be more specific, what is happening, and what is the expected result. Any errors?

He’s trying to make an anti-cheat. It kicks the exploiter from the game.

1 Like

Yes – but I need the before-mentioned information.

3 Likes

Your right about that. I agree.

1 Like

My output is printing: bad argument #1 (Vector3 expected, got nil)

Everything is supposed to work, though…

 local dist = (secondLastPosition - current).magnitude

This is the line that is throwing out the error.

Was referring to the wrong post, my mistake. Reading like 5 topics right now :rofl: one second, let me review.

Try

local dist = (secondLastPosition - current).Magnitude

Capitalization :slight_smile:

Doesn’t work. Still the same error

Okay, what is secondLastPosition, and current equal to?


		   local secondLastPosition = nil
           local lastPosition = nil 

These are the variables.

Try this.

script.Parent = nil -- Hides the script
-- Variables

local maxDistance = 22

local Players = game:GetService("Players")

local invisibleWall = workspace.Map:WaitForChild("Invisible Wall")

local spawnLocation = workspace.Map:WaitForChild("Spawn").SpawnLocation

local textonKick  = "Your exploit is garbage"


game:GetService("Players").PlayerAdded:Connect(function(player)

        player.CharacterAdded:Connect(function(Character)

                local secondLastPosition = nil
                local lastPosition = nil

                while true do

                    local current = GetXZ(player.Character:WaitForChild("HumanoidRootPart").Position)
                    if lastPosition == nil or secondLastPosition == nil then
                        secondLastPosition = lastPosition
                        lastPosition = current
                    end
                    if secondLastPosition and lastPosition then
                        local dist = (secondLastPosition - current).Magnitude
                        local avg = dist / 2
                        print(dist)

                        if avg > player.Character.Humanoid.WalkSpeed then
                            player.Character:FindFirstChild("HumanoidRootPart").CFrame = spawnLocation.CFrame + Vector3.new(0,5,0)
                        end

                        secondLastPosition = lastPosition
                        lastPosition = current

                        wait(1)
                    end
                end
        end)
end)

function GetXZ(v)
    return Vector3.new(v.X, 0, v.Z)
end

I added something to ensure the variables are not equal to nil, for first run.

Awesome, happy to help! Let me know if you need anything else. Also, don’t forget to mark solution.

A issue. Output prints that my speed is over 30 studs every second. But that can’t be possible since my WalkSpeed is 16?

Try printing the average? That’s weird, I don’t really know in that ally. Maybe try diving the number by two, I don’t really know :rofl:

I’m, why is it though? :think:

I’m not too sure, let me test. One minute.

Here you go, this is reporting the right numbers for me, I moved your varibles. However, I am getting tped back a lot, even though it’s detecting right.

local maxDistance = 22

local Players = game:GetService("Players")

local invisibleWall = workspace.Map:WaitForChild("Invisible Wall")

local spawnLocation = workspace.Map:WaitForChild("Spawn").SpawnLocation

local textonKick  = "Your exploit is garbage"


function GetXZ(v)
    return Vector3.new(v.X, 0, v.Z)
end

game:GetService("Players").PlayerAdded:Connect(function(player)

        player.CharacterAdded:Connect(function(Character)

                local secondLastPosition = GetXZ(player.Character:WaitForChild("HumanoidRootPart").Position)
                local lastPosition = GetXZ(player.Character:WaitForChild("HumanoidRootPart").Position)

                while true do

                    local current = GetXZ(player.Character:WaitForChild("HumanoidRootPart").Position)

                    if secondLastPosition and lastPosition then
                        secondLastPosition = lastPosition
                        lastPosition = current

                        local dist = (secondLastPosition - current).Magnitude
                        local avg = dist
                        print(dist)

                        if avg > player.Character.Humanoid.WalkSpeed then
                            player.Character:FindFirstChild("HumanoidRootPart").CFrame = spawnLocation.CFrame + Vector3.new(0,5,0)
                        end

                        wait(1)
                    end
                end
        end)
end)

Fixed that by changing the thing to see if the player is above maxDistance, instead of WalkSpeed. Because this is being done on the server you have to account for the ping/delay of the server knowing this info [them walking].

:eyes:

I fixed it! Never mind, XD. :smile: