Hello there I am making the game of life but the code doesn’t work. I dubgged it and it seems like the problem is the nearby cell detection.
Here is the code:
runService.RenderStepped:Connect(function(dt)
elapsed += dt
while elapsed >= generationDelay do
elapsed -= generationDelay
for _, cell in pairs(array) do
local nearbyCells = {}
local cellsToChange = {}
for _, OtherCell in pairs(array) do
if OtherCell[1] == cell[1] + 4 or OtherCell[1] == cell[1] - 4 or OtherCell[2] == cell[2] - 4 or OtherCell[2] == cell[2] + 4 then
table.insert(nearbyCells, OtherCell)
end
if cell[3]:GetAttribute("IsAlive") == true and #nearbyCells < 2 then
cell[3]:SetAttribute("IsAlive", false)
cell[3].Color = Color3.fromRGB(1,1,1)
elseif cell[3]:GetAttribute("IsAlive") == true and #nearbyCells == 2 or #nearbyCells == 3 then
cell[3]:SetAttribute("IsAlive", true) -- Double checking
cell[3].Color = Color3.fromRGB(255,255,255)
elseif cell[3]:GetAttribute("IsAlive") == true and #nearbyCells > 3 then
cell[3]:SetAttribute("IsAlive", false)
cell[3].Color = Color3.fromRGB(1,1,1)
elseif cell[3]:GetAttribute("IsAlive") == false and #nearbyCells == 3 then
cell[3]:SetAttribute("IsAlive", true)
cell[3].Color = Color3.fromRGB(255, 255, 255)
end
for i, v in pairs(nearbyCells) do
table.remove(nearbyCells, i)
end
task.wait()
end
end
end
end)
This code only works on one cell and doesn’t work for others I don’t know why
Debugged result:
I would like to make this work as fast as possible is there any way I can improve this code aswell?