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
It pretty much deletes the part instantly, unlessif 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!
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.
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.
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)
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.
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?
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?