As I understood you want to add kills or deaths or change the weapon for the players in different times while game is playing.
In your case you should use OOP it's easy to add and remove and change values from and to the table.
You must give an id for every player when he joined your game or your server, Whatever.
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
Now we have a function that gives a data id for the player when he join our game.
So we have add some functions to add kills and get them:
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
Also deaths we should do the same way with them:
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
-- Deaths
function data:AddPlayerDeath(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].deaths = self[playerId].deaths + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new death.
return true
end
return false
end
return false
end
function data:GetPlayerDeaths(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].deaths
end
return false
end
return false
end
And for weapons, We add some functions to change the player weapon and get it as a string.
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
-- Deaths
function data:AddPlayerDeath(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].deaths = self[playerId].deaths + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new death.
return true
end
return false
end
return false
end
function data:GetPlayerDeaths(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].deaths
end
return false
end
return false
end
-- Weapon
function data:SetPlayerWeapon(playerId,newWeapon)
if playerId and type(playerId)=="number" then
if data[playerId] then
if newWeapon and type(newWeapon)=="string" then
data[playerId].weapon = newWeapon
return true
end
return false
end
return false
end
return false
end
function data:GetPlayerWeapon(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].weapon
end
return false
end
return false
end
Finally, Let's try it:
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
-- Deaths
function data:AddPlayerDeath(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].deaths = self[playerId].deaths + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new death.
return true
end
return false
end
return false
end
function data:GetPlayerDeaths(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].deaths
end
return false
end
return false
end
-- Weapon
function data:SetPlayerWeapon(playerId,newWeapon)
if playerId and type(playerId)=="number" then
if data[playerId] then
if newWeapon and type(newWeapon)=="string" then
data[playerId].weapon = newWeapon
return true
end
return false
end
return false
end
return false
end
function data:GetPlayerWeapon(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].weapon
end
return false
end
return false
end
-- Let's try it:
local myPlayer = data:AddNewPlayer() -- returned 1
local myPlayer2 = data:AddNewPlayer() -- returned 2
local myPlayer3 = data:AddNewPlayer() -- returned 3
data:AddPlayerKill(myPlayer) -- We have a specific player with a returned id (myPlayer) and we have add a new kill to his table id.
data:AddPlayerDeath(myPlayer) -- We have a specific player with a returned id (myPlayer) and we have add a new death to his table id.
data:AddPlayerDeath(myPlayer) -- We have a specific player with a returned id (myPlayer) and we have add a new death to his table id.
data:SetPlayerWeapon(myPlayer,"M4") -- We have a specific player with a returned id (myPlayer) and we have changed his weapon to M4.
local playerKills = data:GetPlayerKills(myPlayer)
local playerDeaths = data:GetPlayerDeaths(myPlayer)
local playerWeapon = data:GetPlayerWeapon(myPlayer)
print("Kills: "..playerKills) -- Output : Kills: 1
print("Deaths: "..playerDeaths) -- Output : Deaths: 2
print("Weapon: "..playerWeapon) -- Output : Weapon: M4