Forum

> > CS2D > Scripts > [Solved] How to insert missing values to table?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch [Solved] How to insert missing values to table?

7 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt [Solved] How to insert missing values to table?

Mora
User Off Offline

Zitieren
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:
1
2
3
4
5
6
7
PlayerBasicStats = {
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:
1
2
3
4
Player = {
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
1
2
if not Player[id].Number then
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.
1× editiert, zuletzt 01.06.22 09:06:58

alt Re: [Solved] How to insert missing values to table?

Mora
User Off Offline

Zitieren
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

alt Re: [Solved] How to insert missing values to table?

Mami Tomoe
User Off Offline

Zitieren
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
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

alt Re: [Solved] How to insert missing values to table?

Mora
User Off Offline

Zitieren
Yeah, this thing is exactly what I need.
1
2
3
4
5
local playerEntry = playerData[p]

     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


@user 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 √
1× editiert, zuletzt 01.06.22 09:07:50
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht