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 :
[code]Msg(answers[1] == "nearsea" and (answers[5] == "metal" and "MSG1") or (answers[5] == "wood" and "MSG2"))[/code]: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]
[QUOTE=Crazy Quebec;20855057]Also, in case you wanted to do it all in one line :
[code]Msg(answers[1] == "nearsea" and (answers[5] == "metal" and "MSG1") or (answers[5] == "wood" and "MSG2"))[/code][/QUOTE]
I wouldn't recommend doing this if you are a beginner to Lua.
[QUOTE=MakeR;20855328]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.[/QUOTE]
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... =/
[QUOTE=darksoul47;20855496]Then don't do this. Make something more easy. Like my IMod. If you suicide you get your body ignited... =/[/QUOTE]
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]
[QUOTE=MakeR;20855565]We are supposed to be helping him with his problem, not encourage him to abandon it instead and make something easier.[/QUOTE]
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!
[QUOTE=Crazy Quebec;20855603]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][/QUOTE]
This is a mush better idea, you can easily add new results without the need to add tonnes of if statements.
[QUOTE=MakeR;20855654]This is a mush better idea, you can easily add new results without the need to add tonnes of if statements.[/QUOTE]
Can you explain little more because as I said I am a begginner.
[QUOTE=spjohny;20855686]Can you explain little more because as I said I am a begginner.[/QUOTE]
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
I already did.
[QUOTE=MakeR;20854922]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][/QUOTE]
[QUOTE=MakeR;20896427]I already did.[/QUOTE]
Yes you did :).So adding one more elseif wouldn't be a problem?
Nope.
I cant believe for some reason not matter which combination I choose it always give me the first message (MSG1) the one from the IF.Are the variables defined forever or something because I think they cant change.
[lua]if answers[1] == nearsea then
if answers[5] == metal then
Msg("Fighting Ship")
elseif answers[5] == wood then
Msg("Raft")
end[/lua]
[editline]08:36PM[/editline]
Have a look at the submit button here,something happens and only if I click submit It prints the 1st message.
[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 then
if answers[5] == metal then
Msg("Fighting Ship")
elseif answers[5] == wood then
Msg("Raft")
end
end
[/lua]
nearsea, metal and wood are strings and should be wrapped in quotation marks.
[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" then
if answers[5] == "metal" then
Msg("Fighting Ship")
elseif answers[5] == "wood" then
Msg("Raft")
end
end
[/lua]
I think it is always choosing the first one because you are also setting answers[1] to nearsea, not "nearsea".
That's the button that defined this variable [lua]local answers = {}
local seab = vgui.Create ( "DButton" );
seab:SetSize ( 50 , 20)
seab:SetPos (30 , 55)
seab:SetText( "Near Sea" )
seab:SetParent ( window )
seab.DoClick = function ()
answers[1]="nearsea"
seab:SetDisabled( true )
end
[/lua]
Ps If I put the strings in quotation marks it wont show anything not even the first message.
Are you redefining answers as an empty table every time you create a button?
If you mean that [lua]local answers = {} [/lua] Yes in every button.
Don't do that.
[editline]07:27PM[/editline]
Just do it once at the top of your file (before you make the buttons).
[QUOTE=MakeR;20899271]Don't do that.
[editline]07:27PM[/editline]
Just do it once at the top of your file (before you make the buttons).[/QUOTE]
Omg Dude this works now you are the best thank you very much for your endless help.I get to finish it now.At last the problem was that I put [lua]local answers = {} [/lua] before every button.Thanks,thanks,thanks.
Sorry, you need to Log In to post a reply to this thread.