Help with ore respawning script

So I was working on a system to respawn ores upon them being “deleted” (when an ore gets mined, their parent becomes a folder inside of server storage), but I have found an issue with my current system that ruins the game. When you try to mine one ore, then while that ore is respawning, you mine another ore, what happens is the second ore you mined will never respawn, the reason why this is happening is because of a debounce inside the respawning script, but if I remove that, the respawning becomes super glitchy for some reason.


Server script inside of ServerScriptService:

local db = false

while wait(3) do
	for _,ore in pairs(workspace.ActiveOres:GetChildren()) do
		ore:GetPropertyChangedSignal("Parent"):Connect(function()
			
			for _,spawnBlock in pairs(workspace.SpawnBlocks:GetChildren()) do
				if spawnBlock.Position == ore.Position then

					local ores = game.ServerStorage.OreFolder:GetChildren()
					local randomOre = ores[math.random(1, #ores)]
					local clonedOre = randomOre:Clone()

					coroutine.wrap(function()
						if not db then
							db = true
							print(clonedOre.Name)

							task.wait(clonedOre.SpawnDelay.Value)

							clonedOre.Parent = workspace.ActiveOres
							clonedOre.Position = spawnBlock.Position
							db = false
						end
					end)()
				end
			end
			
		end)
	end
end

Any help is appreciated!

You need to use a dictionary to store the debounces and then process the respawn form there:

local oreDebounce = {}	-- Dictionary to store all Ore Debounces in
--the following in place of "if not db then"
if not oreDebounce[ore] then
	oreDebounce [ore] = true
-- more of your stuff
-- the following in place of "db = false"
oreDebounce[ore] = nil

That should fix it.
Just to check, how are you cleaning up the Connects in this instance as you might get a memory leak if not cleaned up.

2 Likes

That fixed it, but I don’t really understand what you mean about the script leaking memory, to be honest, I don’t really know what a memory leak even is, so could you explain this to me? Thank you. Also sorry for the late response, I went to sleep before you posted your reply.

You can check out another topic on it here

1 Like