So I have made a for loop that creates a new part around 25 times, and then I made it so if it reaches the maximum amount of spawns that it then deletes a part and then spawns it… But I kind of want to make it so it deletes a random part instead of the pervious part.
Now for example if I were to instance all these parts into a folder… and then iterate through them would that work? Or anything remotely. I’m still a beginner in all of this so I am asking questions often lol.
Here’s the gif of how this works.
And here’s the script:
-- Name: partSpawner
-- Written by WizzScout
--[[ Use: This script will spawn parts in a random area at run time.
]]
--Variables
local positionx = 100
local positionz = 100
local height = 10
local size = 5
local numberOfBlocks = 25
local maximumAmount = 0
while true do
for spawner = 1, numberOfBlocks, 1 do
task.wait(0.1)
local newPart = Instance.new("Part")
newPart.Position = Vector3.new(math.random(-positionx, positionx), height , math.random(-positionz, positionx))
newPart.Size = Vector3.new(size, size, size)
newPart.Anchored = true
newPart.Parent = workspace
maximumAmount += 1
if maximumAmount == numberOfBlocks then
task.wait(1)
newPart:Destroy()
maximumAmount -= 1
end
end
If anyone has a better way of doing this and I could learn from it then that would be a godly.
To delete a random part instead of the last one, you can store all the spawned parts in a table and then use the table.remove function to remove a random element from the table. Here’s an updated version of your code that implements this:
--[[ Use: This script will spawn parts in a random area at run time.
]]
-- Variables
local positionx = 100
local positionz = 100
local height = 10
local size = 5
local numberOfBlocks = 25
local maximumAmount = 0
local parts = {}
-- Function to delete a random part from the table of parts
local function deleteRandomPart()
if #parts > 0 then
local index = math.random(#parts)
local partToDelete = parts[index]
table.remove(parts, index)
partToDelete:Destroy()
maximumAmount -= 1
end
end
-- Main loop to spawn and manage parts
while true do
for spawner = 1, numberOfBlocks, 1 do
task.wait(0.1)
local newPart = Instance.new("Part")
newPart.Position = Vector3.new(math.random(-positionx, positionx), height , math.random(-positionz, positionz))
newPart.Size = Vector3.new(size, size, size)
newPart.Anchored = true
newPart.Parent = workspace
maximumAmount += 1
table.insert(parts, newPart)
if maximumAmount > numberOfBlocks then
deleteRandomPart()
end
end
end
In this updated version, we first create an empty table called spawnedParts to store all the spawned parts. Then, each time we spawn a new part, we add it to the table using table.insert. When we reach the maximum number of spawned parts, we select a random index from the spawnedParts table using math.random, destroy the part at that index using :Destroy(), and remove it from the table using table.remove.
I hope this helps! Let me know if you have any more questions.
Could you show an example of putting parts into the table? Kind of struggling and yeah I’m not gonna use that guys answers. Every other post he replies lightning fast with paragraphs of chatgpt answers.
local positionx = 100
local positionz = 100
local height = 10
local size = 5
local numberOfBlocks = 25
local maximumAmount = 0
local partsTable = {}
while true do
for spawner = 1, numberOfBlocks, 1 do
task.wait(0.1)
local newPart = Instance.new("Part")
newPart.Position = Vector3.new(math.random(-positionx, positionx), height , math.random(-positionz, positionx))
newPart.Size = Vector3.new(size, size, size)
newPart.Anchored = true
newPart.Parent = workspace
table.insert(partsTable, newPart)
maximumAmount += 1
if maximumAmount == numberOfBlocks then
task.wait(1)
local randomIndex = math.random(#partsTable)
local partToDelete = partsTable[randomIndex]
partToDelete:Destroy()
table.remove(partsTable, randomIndex)
maximumAmount -= 1
end
end
end
Just to tell you, “external” functions are how you’re supposed to write your scripts to get them organized and better readable… that’s the base of programming lol.