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

Anti-Teleport script won't work and I don't know why?

I'm trying to make a script to stop people from teleporting and I'm not getting any errors in Developer Console but its not working. Do you see anything wrong with the script?

local plr = game.Players.LocalPlayer
local char = plr.Character

while wait() do
	local rootpart = char:FindFirstChild("HumanoidRootPart")
	positionRecorder = {}
	positionRecorder[1] = rootpart.Position
	local pos1 = positionRecorder[1]
	wait(0.1)
	local pos2 = rootpart.Position
	local mag = (pos1 - pos2).Magnitude
	if mag >5 then
		rootpart.CFrame = positionRecorder[1]
	end
end

2 answers

✓ Accepted answer

Instead of recording the position of the rootpart, try to record the CFrame of the rootpart because you are using the CFrame to set the position of character if the player teleports:

local Players = game:GetService("Players")
local player = Players.LocalPlayer

local function antiTP(char)
	local posRecord = {}
		while true do
		table.insert(posRecord,1,char:GetPrimaryPartCFrame)
		local pos1 = posRecord[1].p --Returns Vector3 from CFrame by putting ".p"
		wait(.1)
		local pos2 = char:GetPrimaryPartCFrame.p
		local mag = (pos1 - pos2).magnitude
		if mag > 5 then
			print("Teleport Detected!")
			char:SetPrimaryPartCFrame(posRecord[1])
		end
	end
end

player.CharacterAdded:Connect(antiTP)

Also, since you are going to need to use Vector3 to calculate the magnitude and CFrame to actually set the character back to position before teleport, you will have to save vector3 for pos1 and CFrame for posRecord.

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