I’m trying to create an infinite mining game sort of like Azure Mines, Mining Simulator, REx, you get the idea. I have made a functional block generation system, but after a certain amount of blocks mined, it starts lagging heavily every block mined.
I don’t know exactly what to do to to optimize it.
Instead of looping over all the values, assign the Vector3Value the same name as its value and check for its existence using :FindFirstChild(tostring(newPosition)).
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local POSITIONS = {
Vector3.new(6, 0, 0),
Vector3.new(-6, 0, 0),
Vector3.new(0, 6, 0),
Vector3.new(0, -6, 0),
Vector3.new(0, 0, 6),
Vector3.new(0, 0, -6),
}
local function generateOres(position)
if not position then
return
end
local generated = workspace.Cubes.Generated
for _, offset in ipairs(POSITIONS) do
local newPosition = position + offset
if newPosition.Y > 0 then
continue
end
if generated:FindFirstChild(tostring(newPosition)) then
continue
end
local ore = ServerStorage.Ores.Testorite:Clone()
ore.Position = newPosition
ore.Parent = workspace.Cubes
local value = Instance.new("Vector3Value")
value.Value = newPosition
value.Name = tostring(newPosition)
value.Parent = generated
end
end
local generated = workspace.Cubes.Generated
for _, cube in ipairs(workspace.Cubes:GetChildren()) do
if cube:IsA("Part") then
local value = Instance.new("Vector3Value")
value.Value = cube.Position
value.Name = tostring(cube.Position)
value.Parent = generated
end
end
ReplicatedStorage.BlockGen.OnServerEvent:Connect(function(_, rPart)
generateOres(rPart.Position)
rPart:Destroy()
end)