Bitten Fleax
Bitten Fleax

Reputation: 272

Getting Errors when switching between functions in Lua?

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.

Upvotes: 1

Views: 53

Answers (1)

das
das

Reputation: 547

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

Upvotes: 5

Related Questions