PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Creating a Dance Floor

Creating a Dance Floor

Jun 23 2018, 2:02 AM PST

Problem

You have a Model that contains many parts. You want to randomly change the color of these parts every ¾ of a second to make a dance floor.

Solution

Iterate through the model and use the BrickColor.random() function.

while wait(0.75) do
	for _, v in pairs(game.Workspace.floor:GetChildren()) do
		v.BrickColor = BrickColor.random()
	end
end

Discussion

We first use an infinite while loop to apply this functionality over and over. Within the loop we iterate through the model game.Workspace.floor which should be a model containing only Part|Parts. Then we set the BrickColor of the parts to the result of a new function, BrickColor.random() (this function returns a random brick color).

As a similar example, say we have some hand-picked brick colors that we want to randomly pick from to color the floor. We could do this:

local colors = {"Bright red", "Bright green", "Bright blue"}
while wait(0.75) do
	for _, v in pairs(game.Workspace.floor:GetChildren()) do
		v.BrickColor = BrickColor.new(colors[math.random(1, #colors)])
	end
end

This example defines a colors table outside of the loop and the BrickColor property is set to the brick color returned from BrickColor.new(), a randomly-picked value from the colors table (!math.random() accomplishes this).

Tags:
  • random
  • script