Jump to content

attempt to perform arithmetic on global 'bannT' (a nil v.)


FSXTim

Recommended Posts

Hay,

there is an error is this script. Could you please jave a look at it?

Error: attempt to perform arithmetic on global 'bannT' (a nil value)

function playerConnect(nick) 
    local name = getPlayerFromName(nick) 
    if (tonumber(getElementData(name, "Bann")) ~= 0) then    
        bannT = (tonumber(getElementData(name, "Bann"))) 
        zeit1 = math.floor ( bannT / 60 )..":"..( bannT - math.floor ( bannT / 60 ) * 60 ) 
        zeit2 = ( bannT - math.floor ( bannT / 60 ) * 60 ) 
        if zeit2 > 9 then 
            zeit = math.floor ( bannT / 60 )..":"..( bannT - math.floor ( bannT / 60 ) * 60 )    
        else 
            zeit = math.floor ( bannT / 60 )..":0"..( bannT - math.floor ( bannT / 60 ) * 60 )               
        end 
        cancelEvent(true,"Du bist gebannt! ["..zeit.."]") 
    end 
end  
addEventHandler("onPlayerConnect", getRootElement(), playerConnect) 

Greets

Link to comment
Guest Guest4401

getElementData(name,"Bann") is returning nil, because you never set the element data "Bann" to anything.

Remember: When player quits, the player element is removed, all element datas are cleared.

Link to comment
Guest Guest4401

If BannT is nil after this:

bannT = (tonumber(getElementData(name, "Bann"))) 

It obviously means that the element data "Bann" of the player is nil or it is not a number value.

getElementData(name,"Bann") is returning nil, because you never set the element data "Bann" to anything.
Sure, I did, 'Bann' is saved in the database.

When are you retrieving the data and setting element data?

Link to comment
getElementData(name,"Bann") is returning nil, because you never set the element data "Bann" to anything.

Sure, I did, 'Bann' is saved in the database.

And how can I check the 'Bann'-data otherwise?

Greets

What database? As far as I know, element data is saved nowhere, there's no database.

Link to comment
What database? As far as I know, element data is saved nowhere, there's no database.

I created my own database (mysql).

I will tell you a little bit more about my script.

I created my own admin panel and I have to enter a number. The number is the bann time of the player.

setElementData(source, "Bann", timeOfBann) 
-- the time is saved in my mysql database 

Then the player is disconnected from the server. If he trys to join again, this script is activated:

function playerConnect(nick) 
    local name = getPlayerFromName(nick) 
    if (tonumber(getElementData(name, "Bann")) ~= 0) then    
        bannT = (tonumber(getElementData(name, "Bann"))) 
        zeit1 = math.floor ( bannT / 60 )..":"..( bannT - math.floor ( bannT / 60 ) * 60 ) 
        zeit2 = ( bannT - math.floor ( bannT / 60 ) * 60 ) 
        if zeit2 > 9 then 
            zeit = math.floor ( bannT / 60 )..":"..( bannT - math.floor ( bannT / 60 ) * 60 )    
        else 
            zeit = math.floor ( bannT / 60 )..":0"..( bannT - math.floor ( bannT / 60 ) * 60 )               
        end 
        cancelEvent(true,"Du bist gebannt! ["..zeit.."]") 
    end 
end  
addEventHandler("onPlayerConnect", getRootElement(), playerConnect) 

Greets

Edited by Guest
Link to comment
Guest Guest4401
I think true. Because in the database I can see the bann time, which I set.

When are you retrieving the data from the database and setting element data to the player?

onPlayerConnect is the event triggered when player connects to the server. If you are setting data in onPlayerJoin event, then obviously player's element data will not be set when connected during onPlayerConnect.

Link to comment

Ok, I understand you.

I tried it like this.

Error: attemt to compare number with nil

function playerJoin() 
    setElementData(source, "Bann", tonumber(getElementData(source, "Bann"))) 
    local bt = tonumber(getElementData(source, "Bann")) 
    if bt >= 1 then  
        kickPlayer(source, "Du bist gebannt! ["..zeit.."]") 
    end 
end  
addEventHandler("onPlayerJoin", getRootElement(), playerJoin) 

There has to be a solution to check the row of the username (in the database) which is connecting to the server. If the value is > 0 then cancelEvent, else continue connecting to the server.

Greets

Link to comment
Guest Guest4401

You didn't get me.

As you said, you save the element data in database.

Because in the database I can see the bann time, which I set.

My question: When do you retrieve that data? Which event?

If you never retrieve and set the data, then getElementData(source, "Bann") will always be nil, no matter which event you are using it in because

When player quits, the player element is removed, all element datas are cleared.
Link to comment

I do only retrieve it here:

function playerJoin() 
    setElementData(source, "Bann", tonumber(getElementData(source, "Bann"))) 
    local bt = tonumber(getElementData(source, "Bann")) 
    if bt >= 1 then  
        kickPlayer(source, "Du bist gebannt! ["..zeit.."]") 
    end 
end  
addEventHandler("onPlayerJoin", getRootElement(), playerJoin) 

local bt = tonumber(getElementData(source, "Bann")) 

Greets

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...