Anti-Teleport Script

Made a simple anti-teleport script for my own game but if you’re interested in saving this for yourself here you go. It uses WorldPivot and checks your server-side WalkSpeed to determine when it’s appropriate to teleport the player back to its original position.

local players = game:GetService("Players")
local positions = {}

local function loadplayer(player: Player): ()
	local point
	for _, spawn in pairs(workspace:GetDescendants()) do
		if typeof(spawn) == 'Instance' and spawn:IsA("SpawnLocation") and (spawn.Neutral or spawn.TeamColor == player.TeamColor) then
			point = spawn.Position + Vector3.new(0, 2.5, 0)
			break
		end
	end
	if typeof(point) ~= 'Vector3' then
		point = Vector3.zero
	end
	while true do
		task.wait(.5)
		local character = player.Character
		if typeof(character) == 'Instance' and character:IsA("Model") then
			local humanoid = character:FindFirstChildWhichIsA("Humanoid")
			local walkspeed = 16
			if typeof(humanoid) == 'Instance' and humanoid:IsA("Humanoid") then
				walkspeed = math.min(humanoid.WalkSpeed, 16)
			end
			local pivot = character:GetPivot()
			if not player:FindFirstChild("BypassTeleportRestrictions") and typeof(point) == 'Vector3' and (pivot.Position - point).Magnitude > walkspeed then
				character:PivotTo(CFrame.new(point))
			else
				point = pivot.Position
			end
		end
	end
end

for _, player in pairs(players:GetPlayers()) do
	loadplayer(player)
end

players.PlayerAdded:Connect(loadplayer)

If you have any scenario where you need this disabled, you can create a temporary value (in a server-side module) to bypass the position check.

local module = {}

function module.allowteleportperms(player)
	local bypass = Instance.new("IntValue")
	bypass.Name = 'BypassTeleportRestrictions'
	bypass.Parent = player
end

function module.revoketeleportperms(player)
	for _, bypass in pairs(player:GetChildren()) do
		if bypass.Name == 'BypassTeleportRestrictions' and bypass:IsA("IntValue") then
			bypass:Destroy()
		end
	end
end

return module

This doesn’t account for falling down so if you want to add that go ahead.

12 Likes

It says task.wait(.5), but the walk speed is 16 studs per second, not half second.