Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this material changing script work more than once??

I'm trying to make the following script work more than once it changes a parts material to a random material when I click a TextButton but it only works one time. How do I make it work as many times as I want?

local part = game.Workspace.Stud
local number = math.random(1,21)
script.Parent.MouseButton1Click:Connect(function()

if number == 1 then
    part.Material = "Concrete"
elseif number == 2 then
    part.Material = "Neon"
elseif number == 3 then
	part.Material = "Brick"
elseif number == 4 then
	part.Material = "Glass"	
elseif number == 5 then
	part.Material = "Marble"	
elseif number == 6 then
	part.Material = "Metal"	
elseif number == 7 then
	part.Material = "Foil"	
elseif number == 8 then
	part.Material = "SmoothPlastic"		
elseif number == 9 then
	part.Material = "Fabric"		
elseif number == 10 then
	part.Material = "Ice"		
elseif number == 11 then
	part.Material = "Granite"		
elseif number == 12 then
	part.Material = "Grass"		
elseif number == 13 then
	part.Material = "Pebble"		
elseif number == 14 then
	part.Material = "Sand"		
elseif number == 15 then
	part.Material = "Slate"		
elseif number == 16 then
	part.Material = "Wood"		
elseif number == 17 then
	part.Material = "WoodPlanks"		
elseif number == 18 then
	part.Material = "Brick"		
elseif number == 19 then
	part.Material = "CorrodedMetal"		
elseif number == 20 then
	part.Material = "DiamondPlate"		
elseif number == 21 then
	part.Material = "Cobblestone"		
	end
	end)

3 answers

Move

local number = math.random(1,21)

to inside the function. It will create a random number every time the button is pressed, rather than creating a random number once and only using that number

U can use this

script.Parent.MouseButton1Click:Connect(function()
	local Materials = Enum.Material:GetEnumItems()  --- Find info about materials
	local RandomMaterial = Materials[math.random(#Materials)] ---  Random material

	script.Parent.Material = RandomMaterial   --- Change material
	wait(0.5)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.