Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Kneaks 4
4 years ago

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

Log in to vote
0
Answered by 4 years ago

Place line 2 on line 4

0
Thanks it works! Kneaks 4 — 4y
0
np Brandon1881 721 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)

Answer this question