-1

I need to use a string value as a table, in order to restore points to a player when they reconnect to a game server. This string value is their profile ID, which never changes and I need to put data inside the string value (Kills, deaths, head shots) in order to effectively restore these points. I have had a quick look on the internet but I have not found much because I don't know what this specific thing is actually called. To make it easier, here's what I have so far:

if (not Omega.Playertable) then
    Omega.Playertable = {}; 
    System.LogAlways("Set static record table on first connect");
end
local ID = g_gameRules.game:GetProfileId(player.id);
if (not Omega.Playertable.ID) then
    table.insert(Omega.Playertable, ID);
    Omega.Playertable.g_gameRules.game:GetProfileId(player.id).Kills=0;
    Omega.Playertable.g_gameRules.game:GetProfileId(player.id).Deaths=0;
    Omega.Playertable.g_gameRules.game:GetProfileId(player.id).Headshots=0;
else
    local Kills=Omega.Playertable.g_gameRules.game:GetProfileId(player.id).Kills;
    local Deaths=Omega.Playertable.g_gameRules.game:GetProfileId(player.id).Deaths;
    local Headshots=Omega.Playertable.g_gameRules.game:GetProfileId(player.id).Headshots;
    g_gameRules.game:SetSynchedEntityValue(playerId, 101, Kills);
    g_gameRules.game:SetSynchedEntityValue(playerId, 100, Deaths);
    g_gameRules.game:SetSynchedEntityValue(playerId, 102, Headshots);
end

As you can see, I've tried adding their ID into the table and adding info based on this. I cannot get the system to read the 'ID' value that I set before, so I tried adding the code that gets the ID instead, and it doesn't work. The ID is unique to each player so I cannot use a simple number system for this.

Could someone point out to me what I have done wrong here? If I manage to fix the problem, I will answer my own question on here so that it can be helpful to other users.

AStopher
  • 4,207
  • 11
  • 50
  • 75

2 Answers2

1

Try this:

s="35638846.12.34.45"
id,kills,deaths,headshots=s:match("(.-)%.(.-)%.(.-)%.(.-)$")
print(id,kills,deaths,headshots)

But note that these values are strings. If you're using them as numbers, use tonumber to convert them.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • @AlexanderStopher, I don't really understand what you're trying to do. The code above only shows how to extract the data in the format mentioned in a comment. – lhf Jun 07 '13 at 11:40
1

It seems to me that you are using the wrong table indexing syntax.

Indexing a table by a variables value in Lua is done with the [] syntax.

Furthermore, in Lua Foo.bar is syntactic sugar for Foo["bar"] both formats are interchangeable, but the . variant has limitations on which characters you can use with it. For example Foo["\n.*#%!"] is a valid table index, but you certainly can't write this: Foo.\n.*#%!

Also table.insert(t, v) inserts v at the end of the array part of the table. That means if you do this

foo = {};
foo.X = "Some value";
table.insert(foo, "X");

This is what you get

{
  X   = "Some value"
  [1] = "X"
}

That means, if I apply this to the code you gave us, this is what you probably had in mind:

if (not Omega.Playertable) then
    Omega.Playertable = {}; 
    System.LogAlways("Set static record table on first connect");
end
local ID = g_gameRules.game:GetProfileId(player.id);
if (not Omega.Playertable[ID]) then
    Omega.Playertable[ID] = {};
    Omega.Playertable[ID].Kills=0;
    Omega.Playertable[ID].Deaths=0;
    Omega.Playertable[ID].Headshots=0;
else
    local Kills = Omega.Playertable[ID].Kills;
    local Deaths = Omega.Playertable[ID].Deaths;
    local Headshots = Omega.Playertable[ID].Headshots;
    g_gameRules.game:SetSynchedEntityValue(playerId, 101, Kills);
    g_gameRules.game:SetSynchedEntityValue(playerId, 100, Deaths);
    g_gameRules.game:SetSynchedEntityValue(playerId, 102, Headshots);
end
dualed
  • 10,262
  • 1
  • 26
  • 29