Why is this changing all of my parts to Bright red as opposed to just one?

I’m just fooling around with functions at the moment. I wanted this script to change a part in the model to bright red, then to be parented to Workspace so that it would not be part of the calculation for the next random part selected. Eventually this would continue until all of the parts were red and parented to the workspace. However, instead, almost all of the parts are turned red on the first part touched.


local debounce = false
local release = game.Workspace.Release
local releaseParts = release:GetChildren()

local function openGuiWithParts(part, object)
	if not debounce then
		debounce = true
		if object:FindFirstChild("Humanoid")then
		local randomPart = releaseParts[math.random(1, #releaseParts)]
		randomPart.BrickColor = BrickColor.new("Bright red")
		randomPart.Parent = game.Workspace
		end
		debounce = false
	end
end


for i, v in pairs(releaseParts)do
	v.Touched:Connect(function(part)
		if not debounce then
			openGuiWithParts(v, part.Parent)
		end
	end)
end

The name of the function is a bit misleading, I haven’t started working with the GUI for the moment, but I wanted to make it appear once all four parts are Bright red.

1 Like

It’s because you have this line:

randomPart.BrickColor = BrickColor.new(“Bright red”)

edit: sorry i keep editing im making mistakes with quoting and putting it in lua format.

1 Like

Maybe try adding a debounce, when a player walk’s over a part they will touch it around 20 times, so it might recolor 20 parts at once.

If you want to remove a part from the calculation, make sure you reset the value of releaseParts after parenting a part to the workspace. The table will still contain all parts that were children of release when you set the value of releaseParts.

As for all parts turning red immediately, add a wait(x), where x is some amount of time, before you set debounce back to false.

1 Like

Is there any issues? Or are you good and this is solved?

Just wanna make sure i didnt miss out on anything lol

Yeah, it’s fine now, thank you.

Np! Glad to help you!

adding so i can pass limit

Oh shoot, this was actually what was causing the problem, I had forgotten to add a wait, my original script worked.

1 Like

@itsLevande How would I go about resetting releaseParts? I tried GettingChildren again after parenting to workspace but that didn’t seem to work.


local debounce = false
local release = game.Workspace.Release
local releaseParts = release:GetChildren()

local function openGuiWithParts(part, object)
	if not debounce then
		debounce = true
		if object:FindFirstChild("Humanoid")then
			local randomPart = releaseParts[math.random(1, #releaseParts)]
			randomPart.BrickColor = BrickColor.new("Bright red")
			randomPart.Parent = game.Workspace
			release:GetChildren()
		end
		wait(1)
		debounce = false
	end
end


for i, v in pairs(releaseParts)do
	v.Touched:Connect(function(part)
		if not debounce then
			openGuiWithParts(v, part.Parent)
		end
	end)
end
releaseParts = release:GetChildren()

I’m sorry if what I said was a little bit confusing

1 Like

No need to apologize, your explanation actually helped quite a bit! Thanks again.

1 Like