generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: im trying to understand metatables

Previous Thread :: Next Thread 
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 07:38 PM
so i saw this

Account = {}
function Account.new(balance)
return setmetatable({balance = balance}, Account)
end
Account.__index = Account

so i think, if i say Account.new(5), it will be set to balance = 5?
Report Abuse
ParaText is not online. ParaText
Joined: 15 Aug 2016
Total Posts: 799
20 Aug 2016 07:39 PM
Repost...?
Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 07:41 PM
actually i saw a thread, took this snippet of code and i wanted to try to understand it
Report Abuse
ParaText is not online. ParaText
Joined: 15 Aug 2016
Total Posts: 799
20 Aug 2016 07:42 PM
Ohhh, this video can explain it better than I can:
https://www.youtube.com/watch?v=kLsso_8draE
Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 07:43 PM
aah before you do, can you tell me if that above example was correct though?
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
20 Aug 2016 07:44 PM
Yes OP, a new table is created and in that table "balance" is set to 5.
Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 07:53 PM
{(balance=balance) how does that work?
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
20 Aug 2016 07:53 PM
It's just a table...

local x = {
a = 5
}

print(x.a) --5
Report Abuse
2eggnog is not online. 2eggnog
Joined: 08 Nov 2008
Total Posts: 1351
20 Aug 2016 07:55 PM
"{(balance=balance) how does that work?"

Does this make it clearer?

function Account.new(newBalance)
return setmetatable({balance = newBalance}, Account)
end


Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 07:58 PM
@flux so basically balance is inside balance? Still confused

Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
20 Aug 2016 07:59 PM
No, the key is balance and the value the argument passed.

local function x(y)
local z = {y = y}
print(y.z) -- 3
end

x(3)
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
20 Aug 2016 08:00 PM
print(z.y)***
Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 08:02 PM
I honestly have no idea anymore
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
20 Aug 2016 08:03 PM
It's super duper simple. It's a normal dictionary like

table = {INDEX = VALUE}
print(table.INDEX)

>VALUE

The confusing part (for you) is that the index is called balance and the value (from the parameter) is also called balance.


Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 08:04 PM
Whoops I posted that while you posted. I got it now. One last question y=y??
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
20 Aug 2016 08:06 PM
just imagine this:

function(x)
table = {x=x}
print(table.x)
end

is the same as this:

function(x)
table = {y=x}
print(table.y)
end

Just that the index is different, in the first example the index is X, second example index is Y


Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 08:10 PM
So if you did table{y=x} print(table.y) it would be the same as print(table.x) ?
Report Abuse
thedailyblarg is not online. thedailyblarg
Joined: 26 Feb 2012
Total Posts: 5506
20 Aug 2016 08:14 PM
tables can store variables like anything else can

local tbl = {
x = "hello there"
}

print(tbl.x)

think of it like properties of a part

print(part.Position)

and you can store functions and etc, anything that is a variable
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
20 Aug 2016 08:19 PM
No. What is so confusing to understand. Learn tables before trying to learn metatables.
Report Abuse
aIternatlveaccount is not online. aIternatlveaccount
Joined: 21 Feb 2016
Total Posts: 295
20 Aug 2016 08:39 PM
I'm confused how Y equals X but X doesn't print what the value of y is
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
20 Aug 2016 08:46 PM
You really don't understand what we're trying to explain.

table = {a = "asdf"}
print(table.a)
>asdf

table = {b = "asdf"}
print(table.b)
>asdf

local c = "asdf"
table = {c=c}
print(table.c)
>asdf


Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image