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: metatable problems

Previous Thread :: Next Thread 
gohan31865 is not online. gohan31865
Joined: 30 Jan 2011
Total Posts: 895
16 Sep 2017 11:07 AM
currentsession[player] = data
local private = currentsession[player]
setmetatable(currentsession[player], {
__index = private;
__newindex = function(self, it, value) --this won't fire
rawset(private, it, value)
print("hohohohoho")
end
})


currentsession[player] is another table that stores numbers

a different script is constantly changing those numbers

this snippit is inside of a module script


#code print("your mother")
Report Abuse
kennydies is online. kennydies
Joined: 15 Nov 2008
Total Posts: 825
16 Sep 2017 11:09 AM
Well are you ever setting the values of indices that are nil at first?


Report Abuse
gohan31865 is not online. gohan31865
Joined: 30 Jan 2011
Total Posts: 895
16 Sep 2017 11:13 AM
currentsession[player] = data

data is not nil, and none of the stuff inside it is either


#code print("your mother")
Report Abuse
kennydies is online. kennydies
Joined: 15 Nov 2008
Total Posts: 825
16 Sep 2017 11:16 AM
You said
"currentsession[player] is another table that stores numbers
a different script is constantly changing those numbers"

newindex won't be fired just by changing values.


Report Abuse
gohan31865 is not online. gohan31865
Joined: 30 Jan 2011
Total Posts: 895
16 Sep 2017 05:22 PM
--module example 1 (works)

local module = {}

local module_index = {["number"] = 5}

local meta = setmetatable(module, {
__index = module_index;
__newindex = function(this, it, val)
rawset(module_index, it, val)
print("timed")
end;
})

--module example 2 (wont work)

currentsession[player] = data
local meta = setmetatable(private,{
__index = currentsession[player];
__newindex = function(self, it, value)
rawset(self, it, value)
print(currentsession[player][it])
end
})

i mean it isnt much different, __index is the table with the content, and the table that's being given a metatable is empty, like example 1

only difference i see is that currentsession[player] is a table that has values AND tables inside of it



Report Abuse
gohan31865 is not online. gohan31865
Joined: 30 Jan 2011
Total Posts: 895
16 Sep 2017 05:53 PM
bomp


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
16 Sep 2017 05:58 PM
Yes they are both different.
As multiple people have already explained on your other thread, __newindex is only invoked if t[k] == nil. See that "rawset(self, it, value)" in the second one, yeah.

I highly suggest you stop using metatables because you virtually never need them on roblox.
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