Issues checking player position help

I’m having trouble trying to check if the player’s position changes, I’m not sure how to name my variables, someone could give me a hand :pray:

What I need to do?
I want to check the position of the player, if this player teleports then send a warn () and if the player is normal then just a print showing that everything is fine, could someone help me with improving this function :pray:

script:

local function check_PlayerPosition(currentPosition)
	local current = currentPosition
	local changePosition = current -- i am not sure what to do here...
	
	if(current - changePosition) then
		warn('You are teleporting! warning')
    else
        print('The player is not teleporting, it is normal')
	end
end

game.Players.PlayerAdded:Connect(function(player)
	check_PlayerPosition(player.Position) -- Function called
end)

The problem you have is that you are only checking when a Player is added game.Players.PlayerAdded:Connect(function(player) . After that then it doesn’t check again.

Have you looked at this:

Using State Machines To Mitigate Teleport Exploits

1 Like

The Player Object is not actually your Model that you see inside the workspace here, as it actually belongs inside the Players service whenever you create a server

What you’re looking for is the Character Model of the Player, which is what you want

Both the Player and the Character are 2 different things, and they both belong in separate places apart from each other:

Player - game.Players.Jackscarlett
Character - workspace.Jackscarlett

From your OP issue, you’re trying to track down if a Character is teleporting/moving very fast so that you can issue a warn, what we could possibly do is subtract the 2 values from the old, and the new Position by using Magnitude, which is basically the length of a Vector

Of course though we only want this certain function to fire at specific intervals, so we could possibly use a while true do loop for this instance

local Players = game:GetService("Players")

local function CheckPos(RootPart, OldPos)
    wait(1)
    local NewPos = RootPart.Position

    if (NewPos - OldPos).Magnitude < 20 then
        warn("Someone is teleporting!")
    end
end

Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local RootPart = Character:WaitForChild("HumanoidRootPart")
        local CurrentPos = RootPart.Position

        while RootPart do
            CheckPos(RootPart, CurrentPos)
        end

    end)
end)

I’d assume you’d have to do something along the lines of this in order to check the current distance between the difference of the Character’s old & new positions

1 Like

Oh jack long time no see you here! hope you are well! Thank you very much for your help

do you think you can guide me in the next part, how could I calculate the distance between point a and point b? should I make a function for that?

What do you think about this? Would a function like this be good and what things could improve in this function?

local player = game:GetService("Players")

player.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Character)
		local RootPart = Character:WaitForChild("HumanoidRootPart")
		local CurrentPos = RootPart.Position.X
		local differencePos = CurrentPos * 2
		
		if CurrentPos < differencePos then
			local newPos = differencePos - CurrentPos
			print('current pos is > difference pos ' + newPos)
		else if CurrentPos < differencePos then
				local newPos = differencePos - CurrentPos
				print('current pos is < difference pos ' + newPos)
			end
		end	
	end)
end)
1 Like

Thanks, I appreciate it! I’m not as active as I was on here before though :sweat_smile:

Provided you wanna check for the Distance, you’d need to use Magnitude to know the difference between 2 Vectors

The 1 issue in your code here is that you’re only checking for the X axis of the Character’s RootPart, and not its 3-dimensional position cause the Character could also just only move in the Z axis as well

Judging from what we know, we actually want to save the Character’s old position, and recheck its current position again after a couple of seconds have passed in relation with the old one

local Interval = 1 --To check how quick you wanna see if the Character is moving at an unusual speed or not
local player = game:GetService("Players")

player.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Character)
		local RootPart = Character:WaitForChild("HumanoidRootPart")
		local Humanoid = Character:WaitForChild("Humanoid")

        local function Moving(Speed)
            if Speed > 0 then
                local OldPos = RootPart.Position
                wait(Interval)

                local Magnitude = (RootPart.Position - OldPos).Magnitude

                if Magnitude > 20 then
                    --Do something here
                end 

            end
        end

        Humanoid.Running:Connect(Moving)
	end)
end)