I'm a bit confused on my For Loop which spawns parts

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.
Example

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.

Insert each part into an array (table) then if it gets past your amount set then get the object by using
math.random(1,#numberOfBlocks):Destroy()

Let me know if this doesn’t work

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.

Or ask ChatGPT and copy and paste the answer :skull:

that code will work 100%
stop crying, i made it by my self and i tested it

The comments in the program is crazy

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.

if you want it to be unanchored just do newPart.Anchored = false

thats crazy “local newPart.Anchored = false”

without the local lmao :skull::skull::skull::skull::skull::skull:
so it would be newPart.Anchored = false

Anyway, here.

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
2 Likes

Okay phenomenal. You made a much more sufficient script without having to use external functions unlike someone here using a certain A.I.

And I learned a couple new things here for tables so thank you!

1 Like

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.

1 Like

Sorry I didn’t mean by that at all lol. I meant like the dude using chatgpt just been adding random functions which I didn’t ask for.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.