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

too many elseifs for math.random?

Asked by 4 years ago
1while wait(5) do
2    local r = math.random(1,10)
3    if r == 1 then
4        randomstuff()
5    elseif r == 2 then
6        otherrandomstuff()
7    end
8end

doing this much elseifs makes the code look bigger than it needs to be

how would i make this smaller?

0
idk Coperncius 0 — 4y
1
if you didnt know why did you comment Bloxulen 88 — 4y

2 answers

Log in to vote
2
Answered by
emervise 123
4 years ago
1while wait(5) do
2    local numberTable = 1, 2
3        local r = math.random(1,10)
4for i,v in pairs[numberTable]
5    if v = 1 then
6        randomstuff()
7    if v = 2 then
8        otherrandomstuff()
Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
4 years ago
Edited 4 years ago
01local functions = setmetatable({},{__index = functions})
02 
03functions.first = function()
04print(1)
05end
06 
07functions.second = function()
08print(2)
09end
10 
11local random = Math.random(0,2)
12random = math.floor(random)
13 
14functions[random]()

Answer this question