Intense game-wide lag when cloning rthro body parts

I have a teleportation script in my game. It makes a clone of the player, scales it 1,1,1 bigger, teleports the player, descales it and then deletes it.

Whenever somebody uses this while they have rthro body parts equipped (can be any rthro body part), it causes the entire game to lag for around 10 seconds and nobody can move or do anything until the spike is over. I’ve tried multiple ways of fixing this however I have not yet succeeded.

Here’s the (relevant to my error) code:

piece 1: (teleport function)

local function Teleport(Player, Position)
	local soundEffect = script.Effect:Clone()
	soundEffect.Parent = Player.Character.HumanoidRootPart
	soundEffect.Stopped:Connect(function()
		soundEffect:Destroy()
	end)
	soundEffect:Play()
	wait(0.3)
	for _,v in pairs(Player.Character:GetDescendants()) do
		if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" and v.Name ~= "Collide_Part" and v.Name ~= "Handle" then
			local Clone = v:Clone()
			Clone.CanCollide = false
			Clone.Name = "Transmutation"
			Clone.BrickColor = BrickColor.new("Really black")
			local Face = Clone:FindFirstChildOfClass("Decal")
			if Face then
				Face:Destroy()
			end
			local Face2 = v:FindFirstChildOfClass("Decal")
			if Face2 then
				Face2.Transparency = 1
			end
			Clone.Parent = workspace
			tweenClient:FireAllClients({Clone, 0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, {Transparency = 1, Size = v.Size + Vector3.new(1, 1, 1)}})
			tweenClient:FireAllClients({v, 0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, {Transparency = 1}})
			Debris:AddItem(Clone, 0.3)
			for _,parts in pairs(Player.Character:GetChildren()) do
				if parts:FindFirstChild("IsTransparent") then
					parts.Transparency = 1
				end	
			end
		elseif v.Name == "Handle" then
			tweenClient:FireAllClients({v, 0.4, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, {Transparency = 1}})
		end
	end

piece 2: (calling the function / teleporting the player)

Event.OnServerEvent:Connect(function(Player)
	print("server recieved transmutation")
	local success, errorMessage = pcall(function()
		if Player and Player.Character and Player.Character:FindFirstChild("Head") and Player:FindFirstChild("Transmutation") then
			print("ready for transmuation on server")
			local Position = Mouse:InvokeClient(Player, "Position")
			local Distance, maxDistance = (Position - Player.Character.Head.Position).Magnitude, getMaxDistance(Player)
			if Distance > maxDistance then
				NotificationHandler.Notification(Player, "You are not powerful enough to transmutate that far.")
				return
			end
			local MultiAppa = false
			if Player:WaitForChild("GroupRank").Value >= 200 then
				MultiAppa = true
			end
			if MultiAppa == true and not Player.Character:FindFirstChild("disableMulti") then
				for i,v in next, Players:GetPlayers() do
					if v.Character and v ~= Player then
						if v.Character:FindFirstChild("Head") then
							if (v.Character.Head.Position - Player.Character.Head.Position).Magnitude <= 5 then
								spawn(function()
									local newPos = Position + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
									Teleport(v, newPos)
								end)
							end
						end
					end
				end
			end
			Teleport(Player, Position)
		end
	end)
	if errorMessage then
		warn(errorMessage)
	end
end)

notes: no error messages show at all in output, it just lags for ~10 seconds and then its normal again until someone uses it. i removed the scaling to see if that was the error and it still lagged, so im guessing its the cloning.

It seems to lag as soon as the body parts are cloned.

With all these loops and because you are talking about lags, i would suggest to put the smallest task.wait() possible inside the loop

sorry for the very late response, but where would i put them? in every loop?

I tried adding a task.wait() to the loops and it still lags, the lag seems worse now that i’ve added task.wait()

even here?

for i,v in next, Players:GetPlayers() do
             task.wait()
					if v.Character and v ~= Player then
						if v.Character:FindFirstChild("Head") then
							if (v.Character.Head.Position - Player.Character.Head.Position).Magnitude <= 5 then
								spawn(function()
									local newPos = Position + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
									Teleport(v, newPos)
								end)
							end
						end
					end
				end

yeah, the lag is way worse and it won’t stop, the game is frozen entirely now

What about the Teleport function with one or two wait inside the loop?

Still lagging, still a lot worse.

alright, we will try something now. On the server script (piece 2), comment out the Teleport function. I saw 2 Teleport(). And check if it still lags

which one? theres one after some ends and one before the ends

			if MultiAppa == true and not Player.Character:FindFirstChild("disableMulti") then
				for i,v in next, Players:GetPlayers() do
					if v.Character and v ~= Player then
						if v.Character:FindFirstChild("Head") then
							if (v.Character.Head.Position - Player.Character.Head.Position).Magnitude <= 5 then
								spawn(function()
									local newPos = Position + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
									Teleport(v, newPos) --HERE
								end)
							end
						end
					end
				end
			end
			Teleport(Player, Position) --HERE
		end
	end)
	if errorMessage then
		warn(errorMessage)
	end
end)

These 2 and check if the lag is still here

when i comment them out, nothing happens when i click the keybind

Yeah nothing will happens but the lag is gone? Just to know where to check

yeah there isnt any lag because nothing happens

So the problem here is not the loop on Piece 2 but the loop on Piece 1

You used GetDescendants(), will it still lags alot if you use GetChildren()? (you can add back the Teleport lines on Piece2)

The lag only happens if i leave this line in

Teleport(Player, Position)

theres no lag until that

is this line required or can you remove it? else, is there any way to do that differently?

that line is the one that teleports the player, so it’s needed.