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

How would I randomize non-numerical values?

Asked by
sngnn 274 Moderation Voter
4 years ago

How could I randomize things, like strings or materials?

local part = Instance.new("Part",workspace)
part.Material = Enum.Material.Grass --I want this to be a random value instead of grass, with it still being a material. 

2 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

Actually I checked with tutorials, but it can't random. Apparently you would need to make a table of all the materials and have it so it randomly chooses from the table.

local part = Instance.new("Part",workspace)
TableMaterial = {"Plastic", "Wood", "WoodPlanks", "Slate", "Concrete", "Metal", "CorrodedMetal", "DiamondPlate", "Foil", "Grass", "Ice", "Brick", "Sand", "Fabric", "Granite", "Marble", "Pebble", "Cobblestone", "SmoothPlastic", "Neon", "Glass"}

part.Material = (TableMaterial[math.random(#TableMaterial)])

There you go :)

Ad
Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago

Sorry, but the best way is to just do something like this:

And, loop over it with ipairs, or just follow the example.

local materials = {"Glass", "Wood", "etc."}
local rand = math.random(#materials)
part.Material = Enum.Material[materials[rand]]

Hope That Helped.

0
I tried that but I'm not sure that's too effective, especially with strings. sngnn 274 — 4y
0
There are multiple ways to do it. Like with a Dictionary, like i said, you would want to use ipairs for really any array instead of # since its not that reliable. You probably wont find anythign else besides that. Farsalis 369 — 4y
0
@Qub_lex, you do need to list what you want to randomize. BlackOrange3343 2676 — 4y

Answer this question