Lua help (easy one)

Problem:

How do I make multiple IF’s so if the first block isnt true then check the next and so on?

Tried this but gives me all the time the first Msg

[lua]
if answers[1] == nearsea and answers[5] == metal then
Msg(“MSG1”)
end

if answers[1] == nearsea and answers[5] == wood then
Msg(“MSG2”)
end
[/lua]

using elseif.

[lua]if answers[1] == “nearsea” and answers[5] == “metal” then
Msg(“MSG1”)
elseif answers[1] == “nearsea” and answers[5] == “wood” then
Msg(“MSG2”)
end
[/lua]

You would probably be better doing this:

[lua]if answers[1] == “nearsea” then
if answers[5] == “metal” then
Msg(“MSG1”)
elseif answers[5] == “wood” then
Msg(“MSG2”)
end
end
[/lua]

Does this method works with more than 2 IF’s? For examples I need 10 IF’s total in my script.

Absolutely, though would you mind explaining what you want to do? Maybe we can help you come up with a simple solution.

Also, in case you wanted to do it all in one line :


Msg(answers[1] == "nearsea" and (answers[5] == "metal" and "MSG1") or (answers[5] == "wood" and "MSG2"))

:science:

[editline]02:05PM[/editline]

Yes I know this would print false if the first condition is not met, this is just an example.

Sure here is the explanation.I am making a script which asks some questions ( 7 questions) But so far im using only 2.Whatever the user answers it defined as a variable,The answer variables are 2 answers[1] and answers[5].At the end of the script there is the submit button which makes the consolusion and at the time gives a message.Here are all the IF’s of my script

[lua]
local answers = {}
local sub = vgui.Create ( “DButton” );
sub:SetSize ( 80 , 20)
sub:SetPos (30 , 250)
sub:SetText( “Submit answers” )
sub:SetParent ( window )
sub.DoClick = function ()

if answers[1] == nearsea and answers[5] == metal then
Msg(“Fighting Ship”)
end

if answers[1] == nearsea and answers[5] == wood then
Msg(“Raft”)
end

if answers[1] == nearsea and answers[5] == cement then
Msg(“Sea Base”)
end

if answers[1] == nearland and answers[5] == metal then
Msg(“Armored Tank”)
end

if answers[1] == nearland and answers[5] == wood then
Msg(“Hut”)
end

if answers[1] == nearland and answers[5] == cement then
Msg(“House or Fort”)
end

if answers[1] == intheair and answers[5] == metal then
Msg(“Fighter Plane”)
end

if answers[1] == intheair and answers[5] == wood then
Msg(“Ancient Plane”)
end

if answers[1] == intheair and answers[5] == cement then
Msg(“Skycraper”)
end

if answers[1] == underwater and answers[5] == metal then
Msg(“Submarine”)
end

if answers[1] == underwater and answers[5] == wood then
Msg(“Shipwreck”)
end

if answers[1] == underwater and answers[5] == cement then
Msg(“Underwater base”)
end

end

[/lua]

Are nearsea, underwater etc, supposed to be strings?

[editline]06:15PM[/editline]

I wouldn’t recommend doing this if you are a beginner to Lua.

Yes they should be strings.And yes I am a beginner.

Then don’t do this. Make something more easy. Like my IMod. If you suicide you get your body ignited… =/

We are supposed to be helping him with his problem, not encourage him to abandon it instead and make something easier.

A simpler way would be to put all of your answers in a table.

Adapt to your needs :
[lua]local results = {
nearsea = {
metal = “Fighting Ship”,
wood = “Raft”,
cement = “Sea Base”},
nearland = {
metal = “Armored Tank”,
wood = “Hut”,
cement = “House or Fort”},
intheair = {
metal = “Fighter Plane”,
wood = “Ancient Plane”,
cement = “Skycraper”},
underwater = {
metal = “Submarine”,
wood = “Shipwreck”,
cement = “Underwater base”}
}

Msg(results[“nearsea”][“metal”])[/lua]

Yea I agree, I am dedicated to this script for 2 weeks now I want to finish it,I ve writed 400 lines of code.

It would of course be better to use integers inside the table.

edit : Gaahh! My precious automerge!

This is a mush better idea, you can easily add new results without the need to add tonnes of if statements.

Can you explain little more because as I said I am a begginner.

I will let Crazy do the explaining, I am honestly the worst person at explaining anything.

[editline]06:31PM[/editline]

If he doesn’t mind of course.

Well it depends on what it is you don’t understand…

[lua]table = {“a”,“b”,“c”}
print(table[1]) – This prints the first entry, so “a”[/lua]

[lua]table = {a = 1, b = 2, c = 3}
print(table[“b”]) – This prints the “b” entry, so 2[/lua]

[lua]table = {a = 1, b = 2, c = {12,31,54}}
print(table[“c”][2]) – This prints the second entry of the “c” entry, so 31[/lua]

Ok I got it,but where should I put the code you sent me above?In the submit button?

Can anyone help me please?Where should I put this code?

[lua]
local results = {
nearsea = {
metal = “Fighting Ship”,
wood = “Raft”,
cement = “Sea Base”},
nearland = {
metal = “Armored Tank”,
wood = “Hut”,
cement = “House or Fort”},
intheair = {
metal = “Fighter Plane”,
wood = “Ancient Plane”,
cement = “Skycraper”},
underwater = {
metal = “Submarine”,
wood = “Shipwreck”,
cement = “Underwater base”}
}

Msg(results[“nearsea”][“metal”])

[/lua]

You don’t put it anywhere, that was just an example, a demonstration. You have to come up with a way to integrate it with your forms, if that’s what you want.

Can you just answer my 1st question?How can I add more than 2 IF’s so if one is false it will check the next one and so on,Because I think it will be a little complicated at my level to do your way.

Thanks