Need help with lua addon for Gmod

Hello I have spent the last 2 days designing an addon for Gmod.But I came up with a lua problem.

The question is can I set a button so when I click it,it will define a variable,and If it isnt click it wont?

I tried this with this code

local seab = vgui.Create ( “DButton” );
seab:SetSize ( 50 , 20)
seab:SetPos (10 , 50)
seab:SetText( “Near Sea” )
seab:SetParent ( window )
seab.DoClick = function ()
answer1=“nearsea”
end

But it doesnt seem to work.

Thanks in advance.

What do you mean it doesn’t work? Does it error?

I added this on my lua to test if the variable it defined when I click the button

if answer1==nearsea then
Msg(“lol”)

I click the button and I dont see the message on the console.

Where did you add that code?

In the lua file near the end of the script.

When that piece of code is being run, the answer1 variable isn’t define. answer1 will only be defined after the button is clicked, but that code will be run before. Also:

[lua]if answer1==nearsea then[/lua] should be [lua]if answer1==“nearsea” then[/lua]

So when I click the button will I get “lol” on the console?

No, the code that you added to check if answer1 is “nearsea” will be run once only, before you get a chance to click the button. If you want to check if the variable is being set check it in the buttons click function:

[lua]seab.DoClick = function ()
answer1=“nearsea”
print(answer1)
end[/lua]

Well actually I let me be more specific about the script.There is a question with 3 answers ,each answer has a button.When you click one of the buttons I want it to be assigned as nearthesea for example so I will able to use later on the script with some <if>.So the think I tried to do is when the 1st button is clicked it will be assigned answer1=“nearthesea” so I can use it later on the script.If there another way to do that?Because I want the script to make a conclusion based on the answers the user gives.

Thanks

There is nothing wrong with the way you are doing it, I would just suggest making the answer variable local and declare it beforehand, like this:

[lua]local answer;
– create the buttons
button.DoClick = function()
answer = “whatever”
end[/lua]

Ok thanks a lot for your quick reply,I will check if it works later this day:)

Let me restate my problem

Hello again,I am making a script which besically asks some questions and ends up to a conclusion.To be more specific so far I added all the questions down of each question there are some buttons ,each button is an answer.Here comes the logic problem.I want when a button is clicked (the user is answering a question) to define a variable.So i will be able to use it later on the script with some IF in order to end up to the conclusion.

Here is how i tried to do it but it doesnt seem to work

local seab = vgui.Create ( “DButton” );
seab:SetSize ( 50 , 20)
seab:SetPos (30 , 55)
seab:SetText( “Near Sea” )
seab:SetParent ( window )
seab.DoClick = function ()
answer1=“nearsea”
end

I would really appreciate any help.
Thanks

I don’t see what your problem is, answer1 should now be equal to “nearsea”.

[lua]
– For these kinds of things, a table is better than a gazillion variables
local answers = {}
local seab = vgui.Create ( “DButton”, window );
seab:SetSize ( 50 , 20)
seab:SetPos (30 , 55)
seab:SetText( “Near Sea” )
seab.DoClick = function ()
answers[1] =“nearsea” – Set the first answer, or the first element in the table, to “nearsea”
end

– … more code …

– You use this whenever you’re checking answers.
if answers[1] == “nearsea” then
– THe answer was nearsea
elseif answers[1] == “atsea” then
– The answer was atsea
end[/lua]

Yeah.

Yea but the think is that i want it to be equal to “nearsea” only if the button is clicked.

It will be.

Button.DoClick is an “event” in the sense that it only runs when the button is clicked. The answer is not set to nearsea by default, only when the button is clicked.

How to test that the variavle is defined?

[lua]if ( variable != nil ) then

end[/lua]

So when i run this it should write a messege on console Cool if button NearSea is clicked and Too bad if button nearland is clicked right?

[editline]08:50PM[/editline]

Anyone got an answer?