Parts added to debris in Heartbeat are destroyed once next part is made

EDIT: This is happening anywhere in my game where I use Debris service! Any use of this causes the item to get deleted in one second. I am very frustrated at roblox for having this issue.

Hello,
I am trying to make a dropper. In order to do this I use a heartbeat connection. Here is the following code of importance:

if ore then
     local clone = ore:Clone()
     clone.Anchored = false
     clone:SetAttribute("owner",script.Parent:GetAttribute("owner"))
     clone.Position = script.Parent.Spout.Position
     clone.Parent = workspace.droppedOres
     --wait(2) --these were added to test
     game:GetService("Debris"):AddItem(clone,120)
     --wait(2)
end

Here is a gif of the issue:
https://gyazo.com/a30ea10a09e22f9b9e085b5004d5285f

It pretty much deletes the part instantly, unless if ore is false right after it is created. I’m pretty sure its related to the code looping around but the part gets deleted a second or two before the next part is created. I have tried adding waits as you can see, and deleting the line that adds the ore to Debris fixes the problem. Should I do a workaround and add debris items in another script, or make a coroutine or something? This is disappointing to me, though, so I hope I am missing something!

Thank you

3 Likes

I’m scratching my head here too, but maybe it’s because you’re using GetService() each time? Perhaps try and do it once, maybe that’ll stop it from deleting it instantly.

Something like:

local debris = game:GetService("Debris")
1 Like

Could it be that it is destroyed elsewhere in another script? I.e. an older version of this script you forgot to delete?

3 Likes

Good thought, but this sadly did not work. Probably better practice, though.

Since deleting the line that adds it to debris makes it work properly, no.

1 Like

It seems you are not the only one facing this issue:
Debris problems - Help and Feedback / Scripting Support - DevForum | Roblox

From looking at the other problem, maybe you’re setting the lifetime way too long, maybe try setting it to 10 seconds as a test?

That issue is bizarre because the wait in his script appears to not work properly; I don’t know if that issue is related to mine but the solution isn’t there. I would simply destroy the part if I didn’t want it there for 2 minutes.

I’ll try that, but I have other parts of my game where it’s set to 5 minutes and it works fine.

In that case it probably won’t work :man_shrugging:

It did not, sadly. This is a really frustrating error considering how long it took for me to get the rest of the code to work properly!

Could you try and give me a bit more than the snippet you are showing right now?

1 Like

Here is the entire script.

local ores = {
	["Gold"] = 15,
	["Silver"] = 60,
	["Copper"] = 100,
	["Iron"] = 450
}
local debris = game:GetService("Debris")


local function chooseRandomItem(tbl)
	local total = 0
	for i, v in pairs(tbl) do
		total += v
	end
	local Chance = math.random(1,total)
	local Counter = 0
	for item, weight in pairs(tbl) do
		Counter = Counter + weight
		if Chance <= Counter then
			return item
		end
	end
end




local tm = 0

game["Run Service"].Heartbeat:Connect(function(dt)
	local pwr = false
	for i, v in pairs(script.Parent.power.input:GetTouchingParts())  do
		if v:GetAttribute("wire") == true and v:GetAttribute("powered") == true then
			print("dropper powered")
			pwr = true
			break
		end
	end
	if pwr then
		script.Parent.power.Part.BrickColor = BrickColor.new("Lime green")
		tm += dt
		if tm >= 5 then
			tm = 0
			print("DROPPING ORE")
			local oreType = chooseRandomItem(ores)
			local ore = game.ReplicatedStorage.oreModels:FindFirstChild(oreType)
			if ore then
				local clone = ore:Clone()
				clone.Anchored = false
				clone:SetAttribute("owner",script.Parent:GetAttribute("owner"))
				clone.Position = script.Parent.Spout.Position
				clone.Parent = workspace.droppedOres
				--wait(2) --these were added to test
				debris:AddItem(clone,120)
				--wait(2)
			end
			
		end
		
	else
		script.Parent.power.Part.BrickColor = BrickColor.new("Bright red")
		tm = 0
	end
	
end)


2 Likes

What happens if you add back in the wait()s?
Does it work as intended?

No, it waits two seconds and then deletes the ore, unless the conditions aren’t met after the next 2 seconds. Makes me think it’s actually the next “loop” (I know it isn’t really a loop) that matters; not the current one.

1 Like

Could you try testing out using a while loop instead of heartbeat?

1 Like

I changed the hearbeat to this:

while wait() do
	local pwr = false
	for i, v in pairs(script.Parent.power.input:GetTouchingParts())  do
		if v:GetAttribute("wire") == true and v:GetAttribute("powered") == true then
			print("dropper powered")
			pwr = true
			break
		end
	end
	if pwr then
		wait(5)
		if pwr then
			script.Parent.power.Part.BrickColor = BrickColor.new("Lime green")
			--tm += dt
			--if tm >= 5 then
			--tm = 0
			print("DROPPING ORE")
			local oreType = chooseRandomItem(ores)
			local ore = game.ReplicatedStorage.oreModels:FindFirstChild(oreType)
			if ore then
				local clone = ore:Clone()
				clone.Anchored = false
				clone:SetAttribute("owner",script.Parent:GetAttribute("owner"))
				clone.Position = script.Parent.Spout.Position
				clone.Parent = workspace.droppedOres
				--wait(2) --these were added to test
				debris:AddItem(clone,120)
				--wait(2)
			end

			--end
		end
		
		
	else
		--script.Parent.power.Part.BrickColor = BrickColor.new("Bright red")
		--tm = 0
	end
	
end--)

and it still deletes it as soon as it hits the baseplate. However, it doesn’t have to wait this time when looping so maybe it’s just adding it to the debris at a lower time?

1 Like

As it hits the baseplate? Are there any .Touched events anywhere?

No, sorry. I tried anchoring the part on spawn and it still deletes after about 1/2 of a second. Plus, it’s definitely the debris line. I think this is a bug, unless I’m reaching max parts in debris but since I have times where there’s 20+ parts in debris service and this is only 1 as far as I can tell, I think this is a bug. Should I make a bug report?

1 Like

Yeah, I don’t know what else would cause the problem