Hello everyone!
I’ve been working on a factory in my game that works a lot like a tycoon; a dropper drops items, where they cruise along a conveyor belt until falling into an incinerator.
The objects aren’t just normal bricks, they’re actually random models that range from memes to classic Roblox items.
The issue is, sometimes they can get stuck on corners/snags. And when this happens, it starts a sort of traffic jam. One objects stops the one behind it from passing, which stops the next and so on. In the end, the entire system can get clogged. This not only looks bad, but could be detrimental to the game’s performance, which is why I’ve tried to fix it.
One way I’ve sought to remedy this is by offering a button that allows a player to destroy all the items on the conveyor with a 30 second cooldown, which is working fine and I intend to keep. However, I do not wish to hand the responsibility of keeping one of my game features to the player, which is why I am trying to implement a new system.
My current setup is that all objects are cleared after 3 minutes, set up in a while true loop. While this works fine, I am looking for a way to count the amount of objects in the workspace and, if it exceeds a set number, clear them all. That way, the deletion can happen only when a player decides or when the items exceed a limit.
I already have the code for deleting the objects, I just can’t seem to find a way to check how many there are and destroy them if they exceed a certain value. Does anyone have any ideas how to do this?
Code that automatically destroys the objects after 3 mins:
while true do
wait(180)
for i,v in pairs (workspace:GetDescendants()) do
if v.Name == "Clonedobject" then
v:Destroy()
end
end
print("Destroyed all objects!")
end
code for manual deletion
local ProximityPrompt = script.Parent
local sound = script.Parent.Boom
ProximityPrompt.Triggered:Connect(function(plr)
ProximityPrompt.Enabled = false
for i,v in pairs (workspace:GetDescendants()) do
if v.Name == "Clonedobject" then
v:Destroy()
end
end
sound:Play()
wait(30)
ProximityPrompt.Enabled = true
end)
UPDATE: I thought it would be a good idea to add the code that spawns said items:
local replicatedStorage = game.ReplicatedStorage
while true do
local randomnumber = math.random(0, 16)
if randomnumber == 0 then
main = replicatedStorage.Tycoonparts["hacker"]
end
if randomnumber == 1 then
main = replicatedStorage.Tycoonparts["car"]
end
if randomnumber == 2 then
main = replicatedStorage.Tycoonparts["fly"]
end
if randomnumber == 3 then
main = replicatedStorage.Tycoonparts["cartridecart"]
end
if randomnumber == 4 then
main = replicatedStorage.Tycoonparts["cone"]
end
if randomnumber == 5 then
main = replicatedStorage.Tycoonparts["container"]
end
if randomnumber == 6 then
main = replicatedStorage.Tycoonparts["eye"]
end
if randomnumber == 7 then
main = replicatedStorage.Tycoonparts["fakespawn"]
end
if randomnumber == 8 then
main = replicatedStorage.Tycoonparts["minihouse"]
end
if randomnumber == 9 then
main = replicatedStorage.Tycoonparts["mogus cube"]
end
if randomnumber == 10 then
main = replicatedStorage.Tycoonparts["robonoob "]
end
if randomnumber == 11 then
main = replicatedStorage.Tycoonparts["selectbox"]
end
if randomnumber == 12 then
main = replicatedStorage.Tycoonparts["swordinstone"]
end
if randomnumber == 13 then
main = replicatedStorage.Tycoonparts["troll"]
end
if randomnumber == 14 then
main = replicatedStorage.Tycoonparts["tv"]
end
if randomnumber == 15 then
main = replicatedStorage.Tycoonparts["upsidedown"]
end
if randomnumber == 16 then
main = replicatedStorage.Tycoonparts["brick"]
end
wait(5)
local newobj = main:Clone()
newobj.Name = "Clonedobject"
newobj.Parent = workspace
newobj:PivotTo(game.Workspace.Partdestination.CFrame)
end
(please excuse my primitive way of doing this; it does in fact work and I came up with it by myself! )
Thanks in advance,
~innit_2winnit