[Solved] How to insert missing values to table?
7 replies



31.05.22 06:13:19 pm
Guys, how's possible to check if a playertable has values from other table? For example, the table that is copied when a NEW player is joined:
And for example one is joined but don't have any of them in the table:
How's possible to check if player has no Number, String and Table values? For example through a loop, because such thing like
requires manual coding, but I'd like to make it auto, whenever player joins, so the special code will check if Player has no specific value and insert it.
It required because anytime I add new items in
Any ideas?
Well, Idk if I explained it good enough to understand, but if You have a questions ask.
Code:
1
2
3
4
5
6
7
2
3
4
5
6
7
PlayerBasicStats = {
Mora = 24,
Inventory = {},
Number = 1322,
String = "String",
Table = {1,{s}},
}
Mora = 24,
Inventory = {},
Number = 1322,
String = "String",
Table = {1,{s}},
}
And for example one is joined but don't have any of them in the table:
Code:
1
2
3
4
2
3
4
Player = {
Mora = 24,
Inventory = {},
}
Mora = 24,
Inventory = {},
}
How's possible to check if player has no Number, String and Table values? For example through a loop, because such thing like
Code:
1
2
2
if not Player[id].Number then
Player[id].Number = 1322
Player[id].Number = 1322
requires manual coding, but I'd like to make it auto, whenever player joins, so the special code will check if Player has no specific value and insert it.
It required because anytime I add new items in
PlayerBasicStats
, the player that already has a save file wouldn't have these and I need to delete saves to load all items from PlayerBasicStats
, so I need them to be inserted, if player has no values.Any ideas?
Well, Idk if I explained it good enough to understand, but if You have a questions ask.
edited 1×, last 01.06.22 09:06:58 am
https://stackoverflow.com/questions/55108794/what-is-the-difference-of-pairs-vs-ipairs-in-lua
Isnt pairs exactly what you need?
Isnt pairs exactly what you need?
Code:
1
2
3
4
5
6
2
3
4
5
6
--untested
for k,v in pairs(PlayerBasicStats) do
if (v == nil) then
PlayerBasicStats.k = 1337;
end
end
for k,v in pairs(PlayerBasicStats) do
if (v == nil) then
PlayerBasicStats.k = 1337;
end
end
Share time limited free games here

Something like @
Bowlinghead

Code:
1
2
3
4
5
2
3
4
5
for _, i in pairs(thePlayerOrSth) do
if (_ == 'theKey' and not i) then
thePlayerOrSth.theKey = 1337
end
end
if (_ == 'theKey' and not i) then
thePlayerOrSth.theKey = 1337
end
end
I mean, Is possible to insert items from PlayerBasicStats into Player table, if Player table has no items that PlayerBasicStats has without modifying manually Player table. And anytime I add new item (s) into PlayerBasicStats, the Player table will copy this(these) new item(s) for example in join hook
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
local s = 'example' -- Used below in defaultPlayerData['String'], can be deleted.
local defaultPlayerData = {
Mora = 'noob',
Inventory = { },
Number = 69,
String = "mami best girl",
Table = { 1, { s } },
}
local playerData = {
-- On join:
--[[
init_player(playerId)
]]
-- On leave:
--[[
playerData[playerId] = nil
]]
-- You can create a function for
-- the above, but I decided not to.
}
local function init_player(p)
-- Define player entry.
playerData[p] = { }
-- Load player data from file.
-- !!!LOAD PLAYER DATA FROM FILE HERE!!!
-- !EXACTLY, RIGHT HERE!
-- Set default values.
local playerEntry = playerData[p]
for key, defaultValue in pairs(defaultPlayerData) do
playerEntry[key] = playerEntry[key] or defaultValue
end
end
local defaultPlayerData = {
Mora = 'noob',
Inventory = { },
Number = 69,
String = "mami best girl",
Table = { 1, { s } },
}
local playerData = {
-- On join:
--[[
init_player(playerId)
]]
-- On leave:
--[[
playerData[playerId] = nil
]]
-- You can create a function for
-- the above, but I decided not to.
}
local function init_player(p)
-- Define player entry.
playerData[p] = { }
-- Load player data from file.
-- !!!LOAD PLAYER DATA FROM FILE HERE!!!
-- !EXACTLY, RIGHT HERE!
-- Set default values.
local playerEntry = playerData[p]
for key, defaultValue in pairs(defaultPlayerData) do
playerEntry[key] = playerEntry[key] or defaultValue
end
end

Look at me standing, here on my own again
https://www.lua.org/pil/16.2.html
Just use "inheritance"
Just use "inheritance"
https://ohaz.engineer - Software Engineering
Yeah, this thing is exactly what I need.
In fact I did something like that myself, but I get confused and thought that I'm wrong 100% and this thing wouldn't work. Anyway You're (or Mami or whatever is it) best girl, okay. Just 4 lines and it working like a swiss fucking watch. Thank you

@
ohaz: oh, bro, You ninjad my reply. Well, I'm not experienced enough to understand metatables and OOP thingy(that is my problem and still, mb I'm dumb). But I'll read it fully and try to learn something, thank You.
Thread might be closed, the problem is solved
Code:
1
2
3
4
5
2
3
4
5
local playerEntry = playerData[p]
for key, defaultValue in pairs(defaultPlayerData) do
playerEntry[key] = playerEntry[key] or defaultValue
end
for key, defaultValue in pairs(defaultPlayerData) do
playerEntry[key] = playerEntry[key] or defaultValue
end
In fact I did something like that myself, but I get confused and thought that I'm wrong 100% and this thing wouldn't work. Anyway You're (or Mami or whatever is it) best girl, okay. Just 4 lines and it working like a swiss fucking watch. Thank you

@

Thread might be closed, the problem is solved

edited 1×, last 01.06.22 09:07:50 am
My approach won't work for boolean values perfectly as I didn't use a
If this bothers you, let me know.
nil
check, so if the value is false
it would be turned into the default as well.If this bothers you, let me know.
Look at me standing, here on my own again



