1

This is for a game however I think all of the rules apply. I have a function which is

function nolag()
    if !nolag then 
        nolag = true
        ply:ConCommand("ax_stoplag")
    elseif nolag then 
        nolag = false
        ply:ConCommand("ax_resetlag")
    end
end     

The ply:ConCommand("ax_stoplag") is basically another term for print ("ax_stoplag") in my situation. However I have a button

CreateButton( "No-Lag", MISCtab, black, tblack, true, 355, 130, 95, 20, "Toggle No-Lag on and off", function () nolag() end )

So that will create a button that will then link to the nolag function. However I can click it, but when I click it again nothing happens and I get an error (error in game). And basically the nolag = true and the nolag = false is the problem and is causing the error.

1 Answer 1

5

You defined, nolag as function.

Doing if !nolag then which should be if not nolag then in lua,

basically checks if nolag is not set (if it was not set, then this statement will return true).

Afterwards you are setting nolag (function variable) to true/false that means next button click,

Your application will crash, try one of these 2 options.

local _nolag = false;
function nolag()
    if(not _nolag) then
        _nolag = not _nolag -- or _nolag = true;
        ply:ConCommand("ax_stoplag");
    else -- no need if here, assuming _nolag will always be true or false;
        _nolag = not _nolag
        ply:ConCommand("ax_resetlag");
    end
end


local _nolag = false;
function nolag()
    _nolag = not _nolag;
    ply:ConCommand(_nolag and "ax_resetlag" or "ax_stoplag);
end
Sign up to request clarification or add additional context in comments.

2 Comments

You are a life saver! It works now. Thank-you very much. I can see what I did wrong now I have seen that. Thanks
You've identified the problem but keep in mind: variables don't have types, values do; function nolog()... (when executed) assigns the variable nolag a function value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.