Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Solved] How do you make a player object out of metatables?

I wrote this Module that I know 100% is wrong since it doesn't work. I want to be able to apply the player_properties table to a new player object, but I am struggling to understand how that is going to work. I've looked at all the metatable/metamethod Wikis's on the front page of Google, but I am still confused on what is actually happening. What my end goal is - is to be able to call a function inside of players ( something like players.new() ) that will set a player object with the player_properties attached to it.

I would rather you not write the thing out for me, but tell me what sort of things have to occur to make what I'm trying to do possible.

local player_properties = {name = nil, 
							data = nil, 
							inventory = nil, 
							hotbar = nil, 
							getdata = function(plr) return plr.data end
}

local players = {}

local module = {}
	module.addPlayer = function(player, data)
		players[player] = {player, data}
		setmetatable(players[player], player_properties)
	end
return module

Also should mention that I'm not passing in a player object to player, passing in a String.

3 comments

0

Player is an abstract class. Why would you need to create a new player anyways?

0

Just for better organization and ease of use, and so I can store custom data such as the inventory or hotbar.

0

Also should mention that I'm not passing in a player object to player, passing in a String.

1 answer

If anyone is curious I figured it out...

local player_properties = {name = "Hello", 
							data = nil, 
							inventory = nil, 
							hotbar = nil, 
							getdata = function(plr) return plr.data end,
							
							
}

local index_properties = {__newindex = function(self, index, value) print("New index", self.name, index, value) end,
						__index = function() print("Indexed") end}

local players = {}

local module = {}
	module.addPlayer = function(player, data)
		players[player] = {player}
		setmetatable(players[player], player_properties)
		setmetatable(getmetatable(players[player]), index_properties)
		
		local properties = getmetatable(players[player])
	end

return module

This discussion is preserved as it was. New answers, comments, and votes are closed.