In my game, players can place saplings, that will eventually grow into trees. However, I’m stuck on how to do this efficiently. Brief overview, when a player places a block, I store it in a module, based on the items coordinates. This allows me to go through the module and see what blocks are in what spaces.
Noticeable problems I’m struggling to find solutions to are updating the PlayerCoords module when player has left, or destroyed said sapling. I can’t do like .Changed on an item inside a table. I also don’t think I want to use SetAttribute() every second (seems like it’d create lag?)
But my original idea was to indicate on the block the time it was planted (os.time()) and how long left for it to grow. That way if they leave, I could do CurrentOsTime - PlantedOsTime = TimeLeft
function Tree.Planted(player, sapling, x, y, z)
local PlayersCoords = IslandCoords[player.UserId]
if not PlayersCoords then return end
local Block = PlayersCoords[x][y][z]
local TimeLeft = GrowTime
sapling:SetAttribute("TimeLeft", GrowTime)
sapling:SetAttribute("Planted", os.time())
coroutine.wrap(function()
while wait(1) do
if not player or not player.Parent then break end -- Player left
if PlayersCoords[x][y][z] ~= Block then break end -- Block has changed
if TimeLeft <= 0 then break end -- End of time
TimeLeft -= 1
end
sapling:SetAttribute("TimeLeft", TimeLeft)
if TimeLeft <= 0 then -- Finished growing
PlayersCoords[x][y][z] = "" -- Empty
-- Clone tree objcet
-- Parent tree object to island
-- Add coords
end
end)()
end