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