RandomizedPart Failure

Hey, recently I got an error with my script and I want it to work.

What it has to do:
Get a random number, for example 9.

That means 9 parts out of the RandomizedSpikes table get’s chosen.

After that, they become orange for 3 seconds and then after 2 seconds they’ll get changed back again.

Any fix?

--Variables
local RandomizedSpikes = game.Workspace.Folder.DescendantsMap.RandomizedSpikes:GetChildren()
local RandomNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

--Executor

while true do
	local randomNumber = math.random(1, #RandomNumber)
	local randomizedPart = math.random(randomNumber, #RandomizedSpikes)
	
	print("randomNumber: "..randomNumber, "randomizedPart: "..randomizedPart)

	randomizedPart.BrickColor = BrickColor.new("Neon orange")

	wait(3)

	randomizedPart.BrickColor = BrickColor.new("Medium stone grey")

	wait(2)

end

Error:

attempt to index number with 'BrickColor' 

Thank you.

This is the culprit. This would only return a number, NOT an instance.

1 Like

You’re basically just trying to set randomizedPart, which is a number, to the BrickColor.

To fix that, I’d get the instance from the table instead of the number.

--Variables
local RandomizedSpikes = game.Workspace.Folder.DescendantsMap.RandomizedSpikes:GetChildren()
local RandomNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

--Executor

while true do
	local randomNumber = math.random(1, #RandomNumber)
	local randomizedPart = math.random(randomNumber, #RandomizedSpikes)

	print("randomNumber: "..randomNumber, "randomizedPart: "..randomizedPart)

	RandomizedSpikes[randomizedPart].BrickColor = BrickColor.new("Neon orange")

	wait(3)

	RandomizedSpikes[randomizedPart].BrickColor = BrickColor.new("Medium stone grey")

	wait(2)

end

==================================================
Btw you could’ve set a random number by

local randomNumber = math.random(1,10)

instead of

local RandomNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
local randomNumber = math.random(1, #RandomNumber)

As you see, only 1 part gets changed.

I meant like, if the game chose a random number from 1 to 10, such as 5.

5 parts will get changed, and not 1.

this fails i think
i write this for the limit of words

1 Like

Hm. I get the point. My bad. I’m trying to get it to work right now

Worked halfly in my opinion. Check the video I replied to you

Ah okay i see.

--Variables
local RandomizedSpikes = game.Workspace.Folder.DescendantsMap.RandomizedSpikes:GetChildren()
local RandomNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

--Executor

while true do
	local randomNumber = math.random(1, #RandomNumber)
	local randomizedPart = math.random(randomNumber, #RandomizedSpikes)

	print("randomNumber: "..randomNumber, "randomizedPart: "..randomizedPart)

	
	
	for i,v in pairs(RandomizedSpikes) do
		if i == randomNumber then
			break
		else
			v.BrickColor = BrickColor.new("Neon orange")
		end
	end

	wait(3)
	
	local TimesChangedColor = 0
	for i,v in pairs(RandomizedSpikes) do
		if v.BrickColor == BrickColor.new("Neon orange") then
			randomizedPart.BrickColor = BrickColor.new("Medium stone grey")
			
			if TimesChangedColor >= randomNumber then
				break
			else
				TimesChangedColor += 1
			end
			
		end
	end

	wait(2)

end

Then I would create a for do loop. (Not sure it would work because I haven’t tested it yet)

Edit: wAIT HOLD ON I MADE AN OOPS
Edit2: Done, try it now
Edit3: There was an error but I don’t need to fix it since someone already helped you

I believe this should work for what you want

--Variables
local RandomizedSpikes = game.Workspace.Folder.DescendantsMap.RandomizedSpikes:GetChildren()
local RandomNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

--Executor

while true do
	local randomNumber = math.random(1, #RandomNumber)
	local changed = {}

	for i = 1, randomNumber do
		local id = math.random(1,#RandomizedSpikes)
		RandomizedSpikes[id].BrickColor = BrickColor.new("Neon orange")
		table.insert(changed,RandomizedSpikes[id])
	end

	wait(3)


	for _,v in pairs(changed) do
		v.BrickColor = BrickColor.new("Medium stone grey")
	end

	changed = {}
	
	wait(1)
end

Thank you so much! This works exactly as I wanted it to.

Enjoy your day.

@Crictoli (thank you too.)

Ah no problem, glad you have it fixed anyway.

1 Like