So I made a script that summons parts based on how much you donate, but if you summon 1000 parts or so (they are just studs) it lags and the game slows down. Any way around this or is my game a lost cause?
yeah you’re just making a “how much lag can I cause” game…
Alr good to know. Also nice name XD
I mean you could try to make it use less polygons and simplify your scripts but not much could be done
There are a lot of ways you can do this.
1, Be super strict with your collisions, make sure if you have complex geometry to replace them with Boxcollision boxes if they need collision and turn off the complex geometry collision.
2. Make it so they don’t collide on each other use a very strict collision group
3. If possible No Collide No Touch as many things as you can, and use simplified box part invisible as a bounding box for core part collisions.
4. What you are trying to do here is remove and reduce all events related and systems it has to be loaded in
5. Spawn it client side only and have a server simplified representation
6. When spawning all the parts, create a perframe throttle that will process only so many part creations per a frame, but will keep creating up to that amount and then wait to next frame to complete.
The first thing that is gonna snap you on lag, is trying to create too many things at once on the same frame or too close together, so that’s really what your trying to control =) hope this helps
StreamingEnabled would help, although i don’t use it im sure it would work in your case.
How could I do the thing per frame?
Hook into one of Stepped/Heartbeat, I would suggest look up a queue or stack implementation, likely a queue is your best bet.
You load the queue up with each “request to make a move” so like
Que:Enqueue({Part = Part, ToVector - MoveVectorV3}) – This would mean that your queue has stored the part to move, and the place you want to move it too. Or in your case you don’t need the part just the position you want a created part to go.
In the hooked in stepped/heartbeat location write a function to place here that
warning this is SUDO code! So it is not going to be a copy and past and just work this is just the general things also you can get a basic queue implementation with a little searching online or ChatGPT it
--[[
This function runs all of our part creation requests that are holding
within our queue till it is empty or until it hits 100, if it hits 100 it will stop
and start back up again on the next stepped frame
]]
local processLimit = 100
local function _processCreations()
-- Predicate function to pass over this if we have nothing to process
if Que:isEmpty() == true then return end
local Actions = 0
local tempData = nil
while Que:isEmpty() == false and Actions <= processLimit do
Action += 1
tempData = Que:Dequeue()
SomeFunctionThatCreatesAndMovesThePart(tempData)
end
end
RunService.Stepped:Connect(function(at, dt)
_processCreations()
end)
STAY AWAY FROM THIS PATTERN AT ALL COST
while boolCondition do
doAthing()
if meetSomeCondition() then
task.wait()
end
end
I am not going to go long tail into it, but this is bad, your misusing a while here it’s not a thread your treating it like a heartbeat/stepped but instead of setting it as a function that occurs on the stack, it happens in line and it’s just bad. Trust me here there is a much more technical reason why you shouldn’t do this but trust me.