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.