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

how to make math.random with strings not just numbers?

Asked by 3 years ago

this code: local RandomMap = math.random("GreenForest", "some map", "green desert") it doesnt really work but it only works with numbers any help on how to get it working

1 answer

Log in to vote
5
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

You can do that using arrays, you need to generate number from 1 to size of the array and then pick the generated index from the array, the array will contain strings you need:

local maps = {"GreenForst", "some map", "green desert"}

local random_index = math.random(1, #maps)
local random_map = maps[random_index]

This takes too much lines to write so after bit of shortening you get the following result:

local maps = {"GreenForst", "some map", "green desert"}

local function get_random_map()
    return maps[math.random(1, #maps)]
end
0
^^ kudos Omq_ItzJasmin 666 — 3y
Ad

Answer this question