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

Metatables and Metamethods?

Hi everyone, someone on the Scripting Helpers sub-forum on ROBLOX helped me with a card class script for my card game and this is what I have from modifying it so far:

cardTypes = {
	["0"] = {0,0},
	["1"] = {1,0},
	["2"] = {2,0},
	["3"] = {3,0},
	["4"] = {0,1},
	["5"] = {1,1},
	["6"] = {2,1},
	["7"] = {3,1},
	["8"] = {0,2},
	["9"] = {1,2},
	["DrawTwo"] = {2,2},
	["Reverse"] = {3,2},
	["Skip"] = {4,0},
	["Wild"] = {0,0},
	["DrawFourWild"] = {1,0}
}

cardColours = {
	["Red"] = "rbxassetid://184598259",
	["Blue"] = "rbxassetid://184598232",
	["Green"] = "rbxassetid://184598240",
	["Yellow"] = "rbxassetid://184598224",
	["Wild"] = "rbxassetid://184598210"
}

Card = {};

function Card:new()
	local nCard = {colour = "rbxassetid://184598259", offsetX = 0, offsetY = 0, player = nil}
	self.__index = self
	return setmetatable(nCard, self);
end;

function Card:SetColour(Colour)
	Colour = Colour or "Red";
	self.colour = cardColours[Colour]
end

function Card:SetType(Type)
	Type = tostring(Type) or "1"
	self.offsetX = (cardTypes[Type][1]*174)
	self.offsetY = (cardTypes[Type][2]*236)
end

function Card:SetPlayer(Plr)
	Plr = Plr or nil
	self.player = Plr
end

I would like to know how I would insert the data from the Card:new() function into the Card table so I can use it for setting values when creating a hand for the player in a later part of the script. I was thinking of doing the following, but I'm not sure whether or not to put in self or nCard.

function Card:new()
	local nCard = {colour = "rbxassetid://184598259", offsetX = 0, offsetY = 0, player = nil}
	self.__index = self
	table.insert(Card,self)
	return setmetatable(nCard, self);
end;

Any help is appreciated, thanks!

1 answer

✓ Accepted answer

You actually don't want to put nCard inside Card (by the way, self inside your new function is actually a reference to Card)

When you call Card:new(), you are given the playing card's table as a return value, which you can then put into a table for the Player's Hand. You don't want to mess with the Card table you've created.

3 comments

1

Oh right, thanks. Additional question: How would I go about making it so that when a new card is inserted into the player's hand table, an ImageButton is copied to the player's hand frame with data from that new card?

0

You could try using a proxy table with metamethod hackery. http://www.lua.org/pil/13.4.4.html Otherwise, try copying the image in in the code that inserts the PlayerCard table into the PlayerHand table. If you're got Filtering enabled, use a RemoteEvent.

1

Alright, I'm going to look into it myself again. Thank you.

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