How do i remove lag on looping 500+ objects

tweening? i am not sure if this works it is really similar to the default script

I don’t what you mean. My reply wasn’t to you.

My idea is to have just a single wait call instead of 500, since they all need to wait the same amount of time.

wouldn’t that make the lag longer?

I want to tween all objects at the same time, i tried to remove wait(0.1)
but it lags again

It will be the same ???

the server cannot handle a lot of tweens well so you would have to convert them into the client with remote events

You know what task.spawn is being used for in this case? I dont really use it and so im curious what its being used for here.

This seems like an AI generated code

???
No?

Im just too good???

task.spawn() its like spawn() but better?

You don’t need to use threads for this, just create a function that runs code that doesn’t yield(no task.wait) for multiple objects/instances at once, and seperate those calls from the waits:

local function runMultiple(objects: {Instance}, f: (Instance) -> ())
	for _, object in pairs(objects) do f(object) end
end

--both of these functions should not contain any yielding/waiting
local function f1(object: Instance)
	
end
local function f2(object: Instance)
	
end

local Objects = workspace.Folder:GetChildren()
runMultiple(Objects, f1)
task.wait(2)
runMultiple(Objects, f2)

PS: Tweening objects won’t yield code.

1 Like

What is your goal exactly here? I mean, sure, you’re looping 500+ objects. But what are you doing to these objects exactly? If you’re moving/tweening them, couldn’t you just put all these objects inside of a single model and then move the model instead of moving each part by itself? That way, you only need to move one single thing (the model itself) instead of moving everything, which should reduce lag.

1 Like

From what I understand, you don’t want to use a task.wait() because it would be too slow. Instead, you can make a counter to track what loop number you are on and run a task.wait() every 20 loops or so using the remainder function like so:

if math.fmod(i, 20) == 0 then
	task.wait()
end

In this case, “i” is the loop number and 20 would be how often you want to task.wait(), in this case that would be once every 20 loops. This would prevent your game from freezing when doing large loops

Are you sure it’s lagging because of the looping and not the tweening?

I don’t know what spawn is either…

It creates a new place to run code and not pause the current scope.

task.spawn(function()
task.wait(2) -- something that yields
print("Hello World") -- runs after 2 seconds
end)

print("Hello World") -- runs instantaneously and doesn't wait 2 seconds

Alternatively you could use task.delay for this.

1 Like

Pretty sure it’s lagging because you’re using task.spawn 500 times lol. Just add a loading screen and incorporate this into it

Hi there! I made a topic not too long ago showing how you can move your parts with a high FPS rate! Here is the topic I made.

I hope it isn’t too long, but I do have a TL;DR part for it! I tested a lot of methods and maybe this will help you too! I recommend going through most of it to understand some side info that might be useful to you later as you continue developing! I hope this helps! It will work for tweening too!

You can use a WeldConstraint for the best performance.

what’s the difference between the two?

It’s just that task.delay has a built in task.wait() that you can pass a parameter for.

So instead of this:

task.spawn(function()
task.wait(2)
end)

You can do

task.delay(2, function()

end)
1 Like