local O=game:GetService("\084\101\108\101\112\111\114\116\083\101\114\118\105\099\101")local z=game:GetService("\083\116\097\114\116\101\114\071\117\105")local b=script.Parent.Parent local i=b:FindFirstChild("\084\114\105\103\103\101\114",false)local X=b.Configuration local y=X.Destination.PlaceId local q=b:WaitForChild("\069\118\101\110\116\115")local M=OverlapParams.new()M.FilterType=Enum.RaycastFilterType.Whitelist M.FilterDescendantsInstances={i}q.Teleport.OnServerEvent:Connect(function(z)local b,X=z.Character:GetBoundingBox()local h=z.Character.HumanoidRootPart.Velocity local o=z:GetNetworkPing()+.2 local c=CFrame.new(b.Position+h*o,b.LookVector)local B=c:vectorToObjectSpace(z.Character.PrimaryPart.Velocity)local e=Vector3.new(math.max(math.abs(B.x)/i.Size.x,1),math.max(math.abs(B.y)/i.Size.y,1),math.max(math.abs(B.z)/i.Size.z,1))local V=workspace:GetPartBoundsInBox(c,X*e,M)if next(V)~=nil then O:TeleportAsync(y.Value,{z})else q.Teleport:FireClient(z,false)end end)
local TeleportService = game:GetService('TeleportService');
local StarterGui = game:GetService("StarterGui");

local portal = script.Parent.Parent;
local portalTrigger = portal:FindFirstChild("Trigger", false);
local configuration = portal.Configuration;

local destinationPlaceIdRef = configuration.Destination.PlaceId;

local events = portal:WaitForChild("Events");


local overlapParams = OverlapParams.new();
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist;
overlapParams.FilterDescendantsInstances = { portalTrigger };

events.Teleport.OnServerEvent:Connect(function(player)
	local playerCFrame, playerSize = player.Character:GetBoundingBox();
	local velocity = player.Character.HumanoidRootPart.Velocity;
	local delay = player:GetNetworkPing() + 0.200;
	local scaledCFrame = CFrame.new(playerCFrame.Position + velocity * delay, playerCFrame.LookVector);

	local localVelocity = scaledCFrame:vectorToObjectSpace(player.Character.PrimaryPart.Velocity);
	local graceSize = Vector3.new(
		math.max(math.abs(localVelocity.x) / portalTrigger.Size.x, 1),
		math.max(math.abs(localVelocity.y) / portalTrigger.Size.y, 1),
		math.max(math.abs(localVelocity.z) / portalTrigger.Size.z, 1)
	);

	local overlap = workspace:GetPartBoundsInBox(
		scaledCFrame,
		playerSize * graceSize,
		overlapParams
	);
	if next(overlap) ~= nil then
		TeleportService:TeleportAsync(
			destinationPlaceIdRef.Value,
			{ player }
		);
	else
		events.Teleport:FireClient(player, false);
	end
end);

