I am making a script that will be used for droppers in an upcoming tycoon game of mine. I got them to fundamentally work, but I am having an issue with getting the bricks to clean themselves up after a certain amount of time; I am doing this to prevent lag.
In addition, I am using a popular maid module to help with cleanup/memory loss, but I’m not exactly sure that it is needed in this case, or if I am using it correctly.
Basically, with my script, you click a button, and a dropper drops a brick. After 3 or so seconds, the brick should be removed (as well as any OOP references to it), as this will insure there won’t be any sort of “clogging” with bricks staying around too long. It works, but only if you spawn one brick at a time, like seen in this GIF.
However, if you were to spawn multiple bricks at once, only the most recently spawned brick would be deleted after 3 seconds - the rest would stay there, like seen here.
I’m not sure why this is happening, and it is quite annoying. Does anyone have a solution? Here is my code:
-- the following is the code for clicking to spawn bricks, if that is needed.
script.Parent.ClickDetector.MouseClick:Connect(function()
local dropper = require(game.ServerScriptService.dropperHandler)
dropper.new(100,BrickColor.new("Bright blue"),2,script.Parent.Parent.spawn.Position,"CoolJohnnyboy",workspace)
end)
--- the following is the actual code that spawns the bricks
local drop = {}
local maid = require(game:GetService("ServerScriptService").Maid)
function drop:Demolish()
self.Block:Destroy()
---the maid class didn't delete the actual block, so I had to add the above line.
wait(0.1)
self._maid:Destroy()
end
function drop.new(value: "how much the brick is worth, in $",
color: "brick color value",
size: "how many times bigger than the default brick",
spawnPos: "attachment on dropper",
owner: "username of the player who spawned it",
parent: "where the block will be located")
---local self = setmetatable({}, MyClass)
--- the above was commented out, since referring to everything as "self"
--- here caused issues where I couldn't call the ":Demolish()" function.
drop._maid = maid.new()
drop.Block = script.brick:Clone()
drop.Value = value
drop.Owner = owner
local block = drop.Block
block.Size = block.Size * size
block.BrickColor = color
block.Position = spawnPos
block.Parent = parent
spawn(function()
wait(3)
drop:Demolish()
end)
end
return drop