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

how to use elseif function properly?

how to use elseif function properly?

2 answers

✓ Accepted answer

Elseif can be used to compare two values without having to make multiple if statements. For example I want to compare L to 5 different values. It would be inefficient to use 5 if then statements. We would use elseif so if one if fails, there are others to go off of.

local L = math.random(5) --Picks a random number 1-5
if L == 1 then
    print'Potato'
elseif L == 2 then
    print'Pickle'
elseif L == 3 then
    print'Tomato'
elseif L == 4 then
    print'Lettuce'
elseif L == 5 then
    print'Chocolate'
else
    print'If the script prints this, then something went wrong.'
end

So what the script will do is compare L to 1. If it is not true, then it will go on to the elseif and compare it to 2 and so on until it finds a fitting statement.

######Hope this helped. If so hit the upvote button or accept answer button if it answered your question. Any questions, comment below.

3 comments

0

it wont work on my script for some reason so that why i asked you guys incase i was using it wrong

0

Well, post another question with the script if it still isn't working and I could probably look into it.

0

i went through it and found the problem.the problem was that i had 2 if statements on the top parts so i removed 1 and it works fine now

You bascially just use it like an if statement, so if the first is check is false then it looks for the next one.

local ran = math.random(1,3)

if ran == 1 then
print("one")
elseif ran == 2 then
print("two")
else
print("three")
end

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