Jump to content

Table nil index?


Karevan

Recommended Posts

Hello everyone. I was trying to make my own nick change restriction script because people started to spam with it on my server. Well I wrote the next code, even if i tried many things, I always get the same error:

on line 20 ( nickChangeRestriction[source] = true ) it says table index is nil. It must be an stupid error, but I cant find it. Thanks in advance.

My code:

local nickChangeRestriction = {} 
local timer = {} 
addEventHandler("onPlayerCommand",getRootElement(), 
function(command) 
    if command == "nick" then 
    if nickChangeRestriction[source] then  
        cancelEvent() 
        local a,b,c = getTimerDetails ( timer[source] ) 
        local waitTime = a/1000 
        if waitTime < 60 then 
            waitTime = tostring(math.round(waitTime)).." seconds" 
        else 
            waitTime = tostring(math.round(waitTime/60, 0, "ceil")).." minutes" 
        end 
        outputChatBox("#FF9900*[PM][ANTI FLOOD] #FFFFFFYou have to wait #ABCDEF"..tostring(waitTime).." #FFFFFFto change your nick again",source,255,0,0, true)  
        return 
        end 
  
        --outputChatBox("GTFO",source,255,0,0,true) 
        nickChangeRestriction[source] = true 
        timer[source] = setTimer(function () nickChangeRestriction[source] = false end, 5000, 1) 
        --end 
    end 
end) 
  
function math.round(number, decimals, method) 
    decimals = decimals or 0 
    local factor = 10 ^ decimals 
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor 
    else return tonumber(("%."..decimals.."f"):format(number)) end 
end 

Link to comment

1: You should use the event: "onPlayerChangeNick" instead of "onPlayerCommand", as I'm not sure if you cancel the event, it won't change nick.

2: You only need one table, no need for two tables.

3: Test my code.

local nickChangeRestriction = { } 
  
addEventHandler ( "onPlayerChangeNick", getRootElement(), 
    function ( ) 
        if ( nickChangeRestriction [ source ] and isTimer ( nickChangeRestriction [ source ] ) ) then 
            cancelEvent ( ) 
            local a, b, c = getTimerDetails ( nickChangeRestriction [ source ] ) 
            local waitTime = ( a / 1000 ) 
            local waitTime = ( waitTime < 60 and tostring ( math.round ( waitTime ) ).." seconds" or tostring ( math.round ( ( waitTime / 60 ), 0, "ceil" ) ) .." minutes" ) 
            outputChatBox ( "#FF9900*[PM][ANTI FLOOD] #FFFFFFYou have to wait #ABCDEF".. tostring ( waitTime ) .." #FFFFFFto change your nick again", source, 255, 0, 0, true ) 
            return 
        end 
  
        nickChangeRestriction [ source ] = setTimer ( 
            function ( thePlayer ) 
                nickChangeRestriction [ thePlayer ] = false 
            end 
            ,5000, 1, source 
        ) 
    end 
) 
  
function math.round ( number, decimals, method ) 
    local decimals = ( decimals or 0 ) 
    local factor = ( 10 ^ decimals ) 
    if ( method == "ceil" or method == "floor" ) then 
        return math [ method ] ( number * factor ) / factor 
    else  
        return tonumber ( ( "%.".. decimals .."f" ):format ( number ) ) 
    end 
end 

Link to comment

your code works properly but only if the player changes his name from settings in MTA menu. But what players do is use /nick command. The problem is if I cancel the event on using nick command its impossible to change it from setting too

Edit:

Just changed onPlayerChangeNick to onPlayerCommand, works for both (command and in settings). Heres how the code looks like now, maybe someone wants it:

local nickChangeRestriction = { } 
  
addEventHandler ( "onPlayerCommand", getRootElement(), 
    function ( command ) 
    if command ~= "nick" then return end 
        if ( nickChangeRestriction [ source ] and isTimer ( nickChangeRestriction [ source ] ) ) then 
            cancelEvent ( ) 
            local a, b, c = getTimerDetails ( nickChangeRestriction [ source ] ) 
            local waitTime = ( a / 1000 ) 
            local waitTime = ( waitTime < 60 and tostring ( math.round ( waitTime ) ).." seconds" or tostring ( math.round ( ( waitTime / 60 ), 0, "ceil" ) ) .." minutes" ) 
            outputChatBox ( "#FF9900*[PM][ANTI FLOOD] #FFFFFFYou have to wait #ABCDEF".. tostring ( waitTime ) .." #FFFFFFto change your nick again", source, 255, 0, 0, true ) 
            return 
        end 
  
        nickChangeRestriction [ source ] = setTimer ( 
            function ( thePlayer ) 
                nickChangeRestriction [ thePlayer ] = false 
            end 
            ,300000, 1, source 
        ) 
    end 
) 
  
function math.round ( number, decimals, method ) 
    local decimals = ( decimals or 0 ) 
    local factor = ( 10 ^ decimals ) 
    if ( method == "ceil" or method == "floor" ) then 
        return math [ method ] ( number * factor ) / factor 
    else  
        return tonumber ( ( "%.".. decimals .."f" ):format ( number ) ) 
    end 
end 

Thanks Castillo!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...