I’m working on procedurally generating terrain and I’m using remote events for spawning in parts. Each remote event contains chunk coordinates and chunk data.
For lower chunk sizes and lower render distance this all works fine.
But whenever you increase the chunk size and render distance to somewhat high values the game gets super large stutters every time it has to load in chunks.
I’m wondering if there is a better way to spawn in a bunch of parts. The parts don’t need to spawn in all at once, they just need to not freeze the entire game whenever they are loaded in.
EDIT:
I forgot to include the client-side script so here:
game:GetService("ReplicatedStorage"):WaitForChild("SendChunkPacket").OnClientEvent:Connect(function(coords, chunkData)
local chunkFolder = Instance.new("Folder")
chunkFolder.Name = coords
chunkFolder.Parent = workspace
for i, v in ipairs(chunkData) do
local grass = part:Clone()
grass.Position = v
grass.Parent = chunkFolder
end
end)
Is there a way for my script to not freeze the game and load the parts over time instead of loading them all at once? I really don’t want to have to do the ID verification thing. also i added my script in the original post.
You can still have it computed on the client. You can store the chunk data on the server and render it on the client. This is the best way as if you load all on the server it can cause massive performance issues in the near future.
That’s just how it is, you can do some optimization by using WordRoot:BulkMoveTo() which reduces how many Changed events go off, but you’re going to have to throttle creating parts (or re-use them like the others have said).
you can speed up process by like 10% with RawLib
Also you could cache “common part” with most mutual properties and then just do:
Instance.fromExisting()
yes you got me right Instance.fromExisting() NOT :Clone()
Other than that there pretty much nothing to make it more optimized
Also you can try using :PivotTo() + combined with RawLib it would be even faster
Proofs: Can someone verify this benchmark?
Example:
local RawLib = require("./RawLib")
local instance_index = RawLib.instance_index
local instance_newindex = RawLib.instance_newindex
local commonPart = Instance.new("Part")
local PivotTo = instance_index(commonPart,"PivotTo")::(PVInstance,CFrame)->()
local copy = Instance.fromExisting(commonPart)
PivotTo(copy,CFrame.identity)
instance_newindex(copy,"Parent",workspace)
I dont belive that you need that
You are not moving a model but instead moves individual parts
Non the less :PivotTo is faster and doesn’t require you to bloatware game further
I highly suggest to cache the parts you already created in some table, and when you need to place another part you just get the part from the pool and set position and parent instead of creating a new instance
PVInstance:PivotTo() is intended for moving models, and when you call it on parts that don’t have descendant parts, every changed signal still fires (though this might be a bug?) like if you just set CFrame, but it does seem to be slightly faster than setting .CFrame under most situations.
But what consistently beats both? WorldRoot:BulkMoveTo().