Hello, I am currently making a skinned mesh ocean and I am working on making it endless. The code I have now works but it’s slow and lags the game. Also it’s really messy. Im looking for a way to make it less messy and possibly more efficient. Thanks!
(This is just the code from the chunk part not the water!)
for Index, Mesh in pairs(WaterFolder:GetChildren()) do
if (Mesh.Position - HumanoidRootPart.Position).Magnitude > MESH_SIZE * MESH_RENDER_DISTANCE then
Mesh:Destroy()
end
end
local CurrentPlayerGridPositionX = math.floor((HumanoidRootPart.Position.X / MESH_SIZE) + 0.5) * MESH_SIZE
local CurrentPlayerGridPositionZ = math.floor((HumanoidRootPart.Position.Z / MESH_SIZE) + 0.5) * MESH_SIZE
local CurrentGridMesh = nil
local LeftGridMesh = nil
local RightGridMesh = nil
local FrontGridMesh = nil
local BackGridMesh = nil
local FrontLeftGridMesh = nil
local FrontRightGridMesh = nil
local BackLeftGridMesh = nil
local BackRightGridMesh = nil
for Index, Mesh in pairs(WaterFolder:GetChildren()) do
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX, Mesh.Position.Y, CurrentPlayerGridPositionZ) then
CurrentGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ) then
RightGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX - MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ) then
LeftGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX, Mesh.Position.Y, CurrentPlayerGridPositionZ + MESH_SIZE) then
FrontGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ - MESH_SIZE) then
BackGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX - MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ + MESH_SIZE) then
FrontLeftGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ + MESH_SIZE) then
FrontRightGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX - MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ - MESH_SIZE) then
BackLeftGridMesh = Mesh
end
if Mesh.Position == Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, Mesh.Position.Y, CurrentPlayerGridPositionZ - MESH_SIZE) then
BackRightGridMesh = Mesh
end
end
if CurrentGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX, 0, CurrentPlayerGridPositionZ)
NewMesh.Parent = WaterFolder
CurrentGridMesh = NewMesh
end
if LeftGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX - MESH_SIZE, 0, CurrentPlayerGridPositionZ)
NewMesh.Parent = WaterFolder
LeftGridMesh = NewMesh
end
if RightGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, 0, CurrentPlayerGridPositionZ)
NewMesh.Parent = WaterFolder
RightGridMesh = NewMesh
end
if FrontGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX, 0, CurrentPlayerGridPositionZ + MESH_SIZE)
NewMesh.Parent = WaterFolder
FrontGridMesh = NewMesh
end
if BackGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX, 0, CurrentPlayerGridPositionZ - MESH_SIZE)
NewMesh.Parent = WaterFolder
BackGridMesh = NewMesh
end
if FrontLeftGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX - MESH_SIZE, 0, CurrentPlayerGridPositionZ + MESH_SIZE)
NewMesh.Parent = WaterFolder
FrontLeftGridMesh = NewMesh
end
if FrontRightGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, 0, CurrentPlayerGridPositionZ + MESH_SIZE)
NewMesh.Parent = WaterFolder
FrontRightGridMesh = NewMesh
end
if BackLeftGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX - MESH_SIZE, 0, CurrentPlayerGridPositionZ - MESH_SIZE)
NewMesh.Parent = WaterFolder
BackLeftGridMesh = NewMesh
end
if BackRightGridMesh == nil then
local NewMesh = TemplatesFolder.WaterMesh:Clone()
NewMesh.Position = Vector3.new(CurrentPlayerGridPositionX + MESH_SIZE, 0, CurrentPlayerGridPositionZ - MESH_SIZE)
NewMesh.Parent = WaterFolder
BackRightGridMesh = NewMesh
end