Alright! I got it to work.
I’m going to explain how I did this for anyone who comes across this with the same issue.
First, I grabbed the magnitude between the player and the Corn Model’s hitbox which I added in. I then created a variable called “LoadDist” which we will use later as the maximum magnitude number.
I then created an if statement for “hitboxMagnitude < LoadDist”.
After all of that, I added an attribute to each Corn Model to define its parent, and added the index of the parent CornChunk. (Stored in name, I however suggest using an attribute.)
After all of this, when the Model’s magnitude distance from the player (Magnitude below)
(Hitbox.Position-Torso.Position).Magnitude
is LARGER than the maximum distance (LoadDist) it parents the Corn Model to “ReplicatedStorage”.
When the magnitude is SMALLER than the maximum distance it parents the corn model to its respective chunk via the attributes we set earlier.
CornModel.Parent = CornContainer["CornChunk"..tostring(CornModel:GetAttribute("Chunk"))]:WaitForChild("CornStorage")
I made sure to do all of this on the client to help with memory as well.
Thank you to:
@umamidayo
and
@baseparts
for coming up with ideas to help with this.
Oh and I forgot to mention, this can be applied to many things such as chunk loading for voxel sandbox games, and you can load in the chunks like I did via a grid loader.
(Here is what I did)
local gp = workspace.genpart
local gridamount = 150
local unloaddist = 10
for x = 1,gridamount,gp.Size.X do
for z = 1,gridamount,gp.Size.Z do
local GridPart = gp:Clone()
GridPart.Parent = workspace.Container.grid
GridPart.Anchored = true
GridPart.CanCollide = false
GridPart.Position = Vector3.new(x,0,z)
--print("Loaded chunk:",x,0,z)
end
end
for i,v in pairs(workspace.Container.grid:GetChildren()) do
local new = rs:WaitForChild("CornChunk"):Clone()
new.Parent = workspace.Container.corn
new:PivotTo(v.CFrame)
new.Name = new.Name..tostring(i)
v:Destroy()
--print("Loaded cornchunk:",i)
task.wait() --I suggest making the wait larger if you wish for less lag
end