How can I figure out what number this instance was in a table?

I have a table here for selecting a random part and deleting it, I am trying to remove it from the table so it doesn’t get selected again. But I get this error since I am passing a instance instead of a number

Error:

How do I get around this?

Code:




local PartsFolder = script.Parent.DisapearingParts

local Parts = PartsFolder:GetChildren()

while wait(2) do
	
	local PickedPart = Parts[math.random(1, #Parts)]

	
	table.remove(Parts, PickedPart)
	PickedPart.Color = Color3.fromRGB(255, 0, 0)
	PickedPart.Material = "Neon"
	wait()
	for i = 0, 1.05, 0.05 do
		PickedPart.Transparency = PickedPart.Transparency + 0.05
		wait()
	end
	
	PickedPart.CanCollide = false
	
	
end

1 Like

Try this

Parts[PickedPart]=nil

Either use that, or set the random number to a variable, and use that number in the table.remove function

table.find(table, value) returns the index of the value inside the table which means you can do table.remove(Parts, table.find(Parts, PickedPart))

1 Like

using table.find is redundant; you should store the index in a variable:

local index = math.random(1, #Parts)
local pickedPart = Parts[index]

table.remove(Parts, index)
1 Like

Try replacing

local PickedPart = Parts[math.random(1, #Parts)]

With:

local PickedPart = math.random(1, #Parts)

Edit:
The reason why it was erroring is because you were grabbing a random element from the table.