Making a very fast and anti stuttering portal

Hi,

I’m making a parkour game and it has portals in it. One huge problem is that sometimes, it stutters the player and I feel like it could be ran faster. I tried to fix the stuttering but I didn’t get anywhere, I was thinking it was because the portal was being handled on server side. How do I make this more efficient and maybe fix the stuttering?

for _, Model in pairs(CollectionService:GetTagged("Portal")) do
		if not Model:IsA("Model") then
			continue
		end

		local Portals = {}

		for _, Portal in pairs(Model:GetChildren()) do
			if not Portal:IsA("BasePart") then
				continue
			end
			
			if Portal:GetAttribute("Teleport") == nil then
				continue
			end
			
			table.insert(Portals, Portal)
		end

		local TeleportAttribute_001 = Portals[1]:GetAttribute("Teleport")
		local TeleportAttribute_002 = Portals[2]:GetAttribute("Teleport")
		local previousCharacter = nil :: Model?

		local function Teleport(portal: BasePart, location: BasePart)
			portal.Touched:Connect(function(otherPart: BasePart) 
				if not Players:GetPlayerFromCharacter(otherPart.Parent) then
					return
				end

				local Character = otherPart.Parent :: Model
				local Humanoid = Character:FindFirstChild("Humanoid") :: Humanoid

				if Humanoid.Health == 0 then
					return
				end
				
				if previousCharacter then
					local previousRoot = previousCharacter:FindFirstChild("HumanoidRootPart") :: BasePart
					
					if previousRoot.CFrame == location.CFrame + Vector3.new(0,3,0) then
						return
					end
				end
				
				if portal:FindFirstChildOfClass("Sound") then
					local TeleportSound = portal:FindFirstChildOfClass("Sound")

					PlaySound("rbxassetid://7549600066", portal , game.SoundService.Character, 0.1)
				end

				local Root = Character:FindFirstChild("HumanoidRootPart") :: BasePart

				Root.CFrame = location.CFrame + Vector3.new(0, 3 ,0)
				
				previousCharacter = Character
			end)
		end

		if TeleportAttribute_001 then
			Teleport(Portals[1], Portals[2])
		end

		if TeleportAttribute_002 then
			Teleport(Portals[2], Portals[1])
		end
	end
1 Like