How to minimize lag when updating parts in bulk?

I have a script that updates at least 100 parts at once, to go from their current state to unanchored, visible, and collidable. If I do this script on the client, there is no lag, but on the server, the clients see a sudden lag spike once the parts are updated. I would like to minimize this lag, and I don’t know why it works fine on the client. It can’t be a performance issue (because running the script on the client uses the same computer as running it on the server in a local server environment), unless Roblox has some other stuff going on behind the scenes.

Here is said script:

for _, child in pairs(parent:GetChildren()) do
	if child:IsA("BasePart") then
		child.Anchored = false
		child.Transparency = parent.Transparency
		child.Parent = game.Workspace
		local weld = child:FindFirstChildWhichIsA("WeldConstraint")
		if weld then weld:Destroy() end
		child.CanCollide = true
		local randomPosition = Vector3.new(
			math.random(-0.3, 0.3),
			math.random(-0.3, 0.3),
			math.random(-0.3, 0.3)
		)
		local randomRotation = Vector3.new(
			math.random(-7, 7),
			math.random(-7, 7),
			math.random(-7, 7)
		)
		child.Position = child.Position + randomPosition
		child.CFrame = child.CFrame * CFrame.Angles(
			math.rad(randomRotation.X),
			math.rad(randomRotation.Y),
			math.rad(randomRotation.Z)
		)
		local t = math.random(40, 70) / 10
		Debris(child, t, t - 1)
		local direction = (child.Position - hit.Position).Unit
		child:ApplyImpulse(direction * 15)
		coroutine.wrap(function()
			wait(2)
			while true do
				if child.Velocity.magnitude < 0.001 then
					child.Anchored = true
					child.CanCollide = false
					break
				end
				wait(0.2)
			end
		end)()
	end
end
parent:Destroy()

(there’s some stuff behind the scenes that handles mouse hit target for clicking a part)

1 Like

The only suggestion I have would be to wrap your update function in a coroutine and make it update after every Heartbeat using a while loop.

Perhaps Roblox having to replicate everything on the Server to the Client would cause a lag spike, compared to the Client already updating everything on its side, and not needing to replicate anything to other clients/the server (take this with a grain of salt)

1 Like

There’s no lag using the while loop method, although I kind of need the parts to update instantly. I think the server-to-client method would be a good alternative, but I encountered a lot of server-client desync as I was implementing it, so I think it would be too unreliable to actually use it. I’ll edit the post to include the script, in case you can find a working alternative.

1 Like

Of course, this is faster on the client; nothing has to echo back and forth with the server. Maybe go the other way here and update what you need first and the rest in chunks.

1 Like

I solved it just by making the script execute on the client and have a lesser version execute on the server for other clients to see. Thanks to those who helped!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.