Jump to content

[TUT] Simple ID System


drk

Recommended Posts

Hi all!

Today I will show you how to create a simple noob ID system ( this is only for begginers ).

Now, starting the script:

Create a .lua file.

Open it.

Now we will create a table "ID" and handle a event to on the resource start add a column where will show the players ID.

executeSQLQuery [[ CREATE TABLE IF NOT EXISTS ID ( player TEXT, id NUMBER ) ]] 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
     
    end 
) 

Done! Event handled.

Now we will use function call to add a scoreboard column

Write this:

executeSQLQuery [[ CREATE TABLE IF NOT EXISTS ID ( player TEXT, id NUMBER ) ]] 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        call ( getResourceFromName ( "scoreboard" ), "addScoreboardColumn", "ID List" ) -- We will call 'scoreboard' resource and use addScoreboardColumn to add a new column to the scoreboard 
         
    end 
) 

Where "scoreboard" is your scoreboard resource name.

Ok. Now we will use a for-loop to get all player elements.

executeSQLQuery [[ CREATE TABLE IF NOT EXISTS ID ( player TEXT, id NUMBER ) ]] 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        call ( getResourceFromName ( "scoreboard" ), "addScoreboardColumn", "ID List" ) -- We will call 'scoreboard' resource and use addScoreboardColumn to add a new column to the scoreboard 
        for i,v in ipairs ( getElementsByType "player" ) do -- For-loop 
         
    end 
) 

Now we need only to set ID List column text and save data to the SQL Table.

executeSQLQuery [[ CREATE TABLE IF NOT EXISTS ID ( player TEXT, id NUMBER ) ]] 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) 
        call ( getResourceFromName ( "scoreboard" ), "addScoreboardColumn", "ID List" ) -- We will call 'scoreboard' resource and use addScoreboardColumn to add a new column to the scoreboard 
        for i,v in ipairs ( getElementsByType "player" ) do -- For-loop 
            local sel = executeSQLQuery [[ SELECT * FROM ID WHERE player = '"..getPlayerName(v).."' ]] 
            if ( not sel or #sel == 0 ) then 
                executeSQLQuery [[ INSERT INTO ID VALUES ( '"..sel[1]['player'].."', '"..tostring(id).."' ) ]] 
                setElementData ( v, "ID List", tostring ( i ) ) 
            else 
                setElementData ( v, "ID List", tostring ( i ) ) 
            end 
        end -- end the for-loop 
    end -- end function 
) 

Almost done. Now we need to set the id of new player when join.

addEventHandler ( "onPlayerJoin", root, 
    function ( ) 
        local sel = executeSQLQuery [[ SELECT * FROM ID WHERE player = '"..getPlayerName(source).."' ]] 
        if ( not sel or #sel == 0 ) then 
            for i,v in ipairs ( getElementsByType "player" ) do 
                executeSQLQuery [[ INSERT INTO ID VALUES ( '" .. getPlayerName ( source ) .. "', '"..tostring(_).."' ) ]] 
                setElementData ( source, "ID List", tostring(i) ) 
            end 
        else 
            setElementData ( source, "ID List", sel[1]['player'] ) 
        end 
    end 
) 

Ok. All done.

Complete code:

executeSQLQuery [[ CREATE TABLE IF NOT EXISTS ID ( player TEXT, id NUMBER ) ]] 
  
addEventHandler ( "onResourceStart", resourceRoot, 
    function ( ) -- declare function 
        call ( getResourceFromName ( "scoreboard" ), "addScoreboardColumn", "ID List" ) -- We will call 'scoreboard' resource and use addScoreboardColumn to add a new column to the scoreboard 
        for i,v in ipairs ( getElementsByType "player" ) do -- For-loop 
            local sel = executeSQLQuery [[ SELECT * FROM ID WHERE player = '"..getPlayerName(v).."' ]] 
            if ( not sel or #sel == 0 ) then 
                executeSQLQuery [[ INSERT INTO ID VALUES ( '"..sel[1]['player'].."', '"..tostring(id).."' ) ]] 
                setElementData ( v, "ID List", tostring ( i ) ) 
            else 
                setElementData ( v, "ID List", tostring ( i ) ) 
            end 
        end -- end the for-loop 
    end -- end function 
) 
  
addEventHandler ( "onPlayerJoin", root, 
    function ( ) 
        local sel = executeSQLQuery [[ SELECT * FROM ID WHERE player = '"..getPlayerName(source).."' ]] 
        if ( not sel or #sel == 0 ) then 
            for i,v in ipairs ( getElementsByType "player" ) do 
                executeSQLQuery [[ INSERT INTO ID VALUES ( '" .. getPlayerName ( source ) .. "', '"..tostring(_).."' ) ]] 
                setElementData ( source, "ID List", tostring(i) ) 
            end 
        else 
            setElementData ( source, "ID List", sel[1]['player'] ) 
        end 
    end 
) 

Don't know how to create the XML?

<meta> 
     
    <info author = 'Your name' description = 'Description goes here' version = 'Version goes here' type = 'Type: misc / script / map / gamemode' /> 
     
    <script src = 'file.lua' type='server' /> 
     
</meta> 

Ok. All done.

If you do not understand, you can use my Resource Creator ^^

If you have any problems, don't hesitate to contact us.

And next time, I will try to make a better tutorial :S

Edited by Guest
Link to comment
addEventHandler ( 'onPlayerJoin', root, 
    function ( ) 
        for i, v in ipairs ( getElementsByType ( 'player' ) ) do -- For-loop again to get all elements 
            setElementData ( v, 'ID List', tostring(i) ) -- set all id's again 
        end -- end the for-loop 
    end -- close the function 
) 

doesn't this means that almost every time an player quit and another player join will make your id changing? lol

Link to comment
addEventHandler ( 'onPlayerJoin', root, 
    function ( ) 
        for i, v in ipairs ( getElementsByType ( 'player' ) ) do -- For-loop again to get all elements 
            setElementData ( v, 'ID List', tostring(i) ) -- set all id's again 
        end -- end the for-loop 
    end -- close the function 
) 

doesn't this means that almost every time an player quit and another player join will make your id changing? lol

Yeah, if you leave the server and another player joins, when you join again your ID will change.

Link to comment
addEventHandler ( 'onPlayerJoin', root, 
    function ( ) 
        for i, v in ipairs ( getElementsByType ( 'player' ) ) do -- For-loop again to get all elements 
            setElementData ( v, 'ID List', tostring(i) ) -- set all id's again 
        end -- end the for-loop 
    end -- close the function 
) 

doesn't this means that almost every time an player quit and another player join will make your id changing? lol

Yeah, if you leave the server and another player joins, when you join again your ID will change.

not this what i mean

i mean if other player quit and another player join your ID will change

example:

player A join the server first he will have ID 1

player B join the server after A join he will have ID 2

player C join the server after B join he will have ID 3

player D join the server after C join he will have ID 4

player F join the server after D join he will have ID 5

now if player A quit from the server and then player J join the server, player B and player C and player D and player F will have new ID (his ID will changed).

Link to comment
  • Recently Browsing   0 members

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