I’ve looked at several forum posts and I can’t find a good solution.
I have this giant loop that loads a players “vehicle” and transfers it from build mode to flight mode. It runs at an okay speed, but it lags the entire game if the player has a build that has more than 400-700 blocks.
Is there any way I can fix this? I have tried coruntining everything, but that just broke a whole bunch of functions.
for i, v in pairs(plot:GetChildren()) do
if v.ClassName ~= "SelectionBox" and v.ClassName ~= "SurfaceGui" then
local clone = game.ReplicatedStorage.Items:FindFirstChild(v.Name)
if clone then
clone = clone:Clone()
else
if string.find(string.lower(v.Name),"customshape") then
clone = v:Clone()
clone:SetAttribute("Health",100)
local weld = Instance.new("WeldConstraint",clone.W1)
weld.Part0 = clone.W1
weld.Part1 = clone.W2
else
clone = nil
end
end
if clone then
pcall(function()
clone:SetPrimaryPartCFrame(v:GetPrimaryPartCFrame())
end)
pcall(function()
if clone:FindFirstChild("Config") then
clone.Config:ClearAllChildren()
for o,b in pairs(v.Config:GetChildren()) do
local clone2 = b:Clone()
clone2.Parent = clone.Config
end
end
for o, b in pairs(clone:GetChildren()) do
if b.ClassName == "Part" or b.ClassName == "WedgePart" or b.ClassName == "MeshPart" or b.ClassName == "CornerWedgePart" or b.ClassName == "UnionOperation" then
b.Material = v:FindFirstChild(b.Name).Material
b.Color = v:FindFirstChild(b.Name).Color
b.Transparency = v:FindFirstChild(b.Name).Transparency
end
end
end)
if clone then
clone.Parent = model
end
end
v:Destroy()
end
end
-- end)
--coroutine.resume(starter1)
--functionlist[#functionlist+1] = starter1
pcall(function()
p.Character.Humanoid.Jump = true
end)
local plane = model:Clone()
plane.Parent = workspace
local angles = plane:GetPivot()-plane:GetPivot().Position
if plane:FindFirstChild("Seat") then
local seat = plane:FindFirstChild("Seat")
print(seat.Name)
angles = seat.Seat.CFrame-seat.Seat.CFrame.Position
end
angles = CFrame.fromEulerAnglesYXZ(angles.X,angles.Y,angles.Z+math.rad(90))
local mass = Instance.new("NumberValue",plane)
mass.Name = "Mass"
mass.Value = 0
for i, v in pairs(plane:GetDescendants()) do
if v.ClassName == "Script" then
v.Enabled = true
elseif v.ClassName == "Part" or v.ClassName == "Wedge" or v.ClassName == "Seat" then
mass.Value += v.Mass
end
end
--for i, v in pairs(plane:GetChildren()) do
-- for _,w in pairs(v:GetChildren()) do
-- pcall(function()
-- w.Anchored = false
-- end)
-- end
--end
for i, v in pairs(plane:GetChildren()) do
if math.random(1,5) == 1 then
game.ReplicatedStorage.Events.Misc.ProgressBars.UpdateProgressBar:FireClient(p,i,#plane:GetChildren())
end
if #plane:GetChildren() >= 500 then
if math.random(1,45) == 1 then
wait(.08)
end
end
for o,b in pairs(v:GetChildren()) do
if string.find(string.lower(b.ClassName),"part") or string.find(string.lower(b.ClassName),"union") then
local originalsize = b.Size
--b.Size = b.Size + Vector3.new(.05,.05,.05)
local touching
--b.Size = originalsize
if v.Name == "Motor" or v.Name == "Motor2" then
if v.Config.Legacy.Value == true then
if b.Name == "Base" then
touching = getAdjacentParts(b,{Position = v.Shape.Position,Size = Vector3.new(3,3,3)})
end
else
touching = getAdjacentParts(b)
end
else
touching = getAdjacentParts(b)
end
if not touching then
touching = {}
end
for p,n in pairs(touching) do
if n:IsDescendantOf(plane) then
if not n:IsDescendantOf(v) then
if n.Name ~= "Shape" and b.Name ~= "Shape" then
local weld = Instance.new("WeldConstraint",n)
weld.Part0 = n
weld.Part1 = b
end
end
end
end
end
end
if v.Name == "Motor" then
if v.Config.Visible.Value == false then
v.Base.CanCollide = false
v.Turnable.CanCollide = false
end
end
if v.Name == "InvisibleBlock" then
if v.Config.CanCollide.Value == false then
for _,q in pairs(v:GetChildren()) do
if q:IsA("Part") then
q.CanCollide = false
end
end
end
end
if v.Name == "InvisibleWingPanel" then
if v.Config.CanCollide.Value == false then
for _,q in pairs(v:GetChildren()) do
if q:IsA("Part") then
q.CanCollide = false
end
end
end
end
end
--local starter2 = coroutine.create(function()
if location == "Water" then
plane:PivotTo(CFrame.new(workspace.Spawns.Water:GetChildren()[math.random(1,#workspace.Spawns.Water:GetChildren())].Position)*angles)
end
for i, v in pairs(plane:GetChildren()) do
for _, t in pairs(v:GetChildren()) do
pcall(function()
t.Anchored = false
end)
end
end
local functionsetups = {}
for i, v in pairs(plane:GetChildren()) do
if math.random(1,25) == 1 then
game.ReplicatedStorage.Events.Misc.ProgressBars.UpdateProgressBar:FireClient(p,i,#plane:GetChildren())
end
if math.random(1,75) == 1 then
task.wait(0.05)
end
local s, e = pcall(function()
if setups[v.Name] then
local spawned = coroutine.create(function() setups[v.Name](p,v) end)
coroutine.resume(spawned)
table.insert(functionsetups,(#functionsetups+1),spawned)
--wait(0)
--coroutine.close(spawned)
end
end)
for _,w in pairs(v:GetChildren()) do
pcall(function()
w:SetNetworkOwner(p)
end)
end
if s then
-- print("all objects on client side")
else
-- print(e)
end
end
wait()
for i, v in pairs(functionsetups) do
coroutine.close(v)
end