- I decided to modify my Top Killer system, but I had problems like this: How to sorting?
- I used a method I found here in the forum: Unexpected behaviour of Squirrel tables
And I created a Top Killer system without using database every time it is called:
Create a table:
Code Select
Accounts <- {}; /* Table where accounts will be */
Functions:
Code Select
/* Let's say we are loading the accounts from the database */
function loadAccounts() {
//9 "accounts"
Accounts.rawset(1, {Name = "Player1", Kills = rand() % 1000});
Accounts.rawset(2, {Name = "Player2", Kills = rand() % 1000});
Accounts.rawset(3, {Name = "Player3", Kills = rand() % 1000});
Accounts.rawset(4, {Name = "Player4", Kills = rand() % 1000});
Accounts.rawset(5, {Name = "Player5", Kills = rand() % 1000});
Accounts.rawset(6, {Name = "Player6", Kills = rand() % 1000});
Accounts.rawset(7, {Name = "Player7", Kills = rand() % 1000});
Accounts.rawset(8, {Name = "Player8", Kills = rand() % 1000});
Accounts.rawset(9, {Name = "Player9", Kills = rand() % 1000});
/* NOTE: You will have to adapt this function to load all the accounts of your database. */
}
function topKills() {
/*Here, we will temporarily clone the original table, because we will
have to delete some data (and we do not want this to happen in the main table)*/
local backup_Accounts = clone Accounts;
local limit = 5, tmp = "";
while (limit) {
local topScore = 0, index = 0;
foreach (i, v in backup_Accounts)
{
if (backup_Accounts[i]["Kills"] > topScore) {
topScore = backup_Accounts[i]["Kills"];
index = i;
}
}
if (index) tmp = tmp + backup_Accounts[index]["Name"] + " - " + backup_Accounts[index]["Kills"] + ", ";
backup_Accounts.rawdelete(index);
limit --;
}
return tmp != "" ? tmp.slice(0, tmp.len() - 2) : "No top killers";
}
function getAccountID(str) {
foreach (i, v in Accounts) {
if (Accounts[i]["Name"].tolower() == str.tolower()) {
return i;
break;
}
}
return 0;
}
Server Events:
Code Select
function onScriptLoad() {
loadAccounts();
print(topKills());
}
function onPlayerKill(killer, player, reason, bodypart) {
/* Do not forget to update the amount of kills when the player scores */
local i = getAccountID(killer.Name);
if (i) Accounts[i]["Kills"] ++;
}
Result:

My server:

* I do not know why it always starts with the same values, even using the rand(), but you can change each kill in the loadAccounts function to confirm the script's operation (Someone explains to me)