Hey devs! I want to make a database. To make my database I got to make a table named "session_data" like that.

local PlayerService = game:GetService('Players')

local session_data = {}

PlayerService.PlayerAdded:Connect(function(player)
	session_data[player.UserId] = {
		data = {
			currency = {
				money = 0
			},
			last_name = ''
		}
	}
end)

So, now I want set the table to metatable. Like that

local PlayerService = game:GetService('Players')

local session_data = {}

setmetatable(session_data, {
	__index = function(tlb, ...) print(select(1, ...)) end;
	__newindex = function(tlb, ...) print(select(1, ...)) end
})

PlayerService.PlayerAdded:Connect(function(player)
	session_data[player.UserId] = { -- newindex called
		data = {
			currency = {
				money = 0
			},
			last_name = ''
		}
	}
end)

The problem is than new table index is not create. If I want change the last name for the actual name of the client the output return

attempt to index field '?' (a nil value)

How to fix this?

3 comments

0

You did __newindex function() instead of __newindex = function().

0

It's just a write mistake, I know that, it's not my problem

0

The script is good, but I don't know how to create a new index in my metatable and how to change the value in that.