#Metatables are complicated

1 messages · Page 1 of 1 (latest)

solemn turtle
#

basicly I want many stats of the player to be in one table (like health...) in a module but in a normal table u cant detect if a value changes so im usign metatables but its not really working

I have this module script:

local module = {}

local rs = game:GetService("RunService")

local Values = {
    ["val1"] = false,
    ["val2"] = true
}

module.mt = {}

setmetatable(Values,module.mt)

print(module.mt)

module.SetValue = function (ValueName, NewValueValue)
    if rs:IsServer() == true then
        print(2)
        module.mt[ValueName] = NewValueValue
    end
end

return module

and this serverscrpt:

local module = require(game.ReplicatedStorage.Modules.ValueScript)

wait(1)

module.SetValue("val1",true)

print(module.mt)

wait(1)

but the table is empty until I change a value in it with: module.SetValue("val1",true)

so it prints:

20:54:34.262 {} - Server - ValueScript:14
20:54:35.265 2 - Server - ValueScript:18
20:54:35.265 ▼ {
["val1"] = true
} - Server - Script:7

why is that

umbral spade
#

Because u told it to be?

solemn turtle
umbral spade
#

You don't have any metamethods

#

Also I think thats backwards for what u want

solemn turtle
#

idk I dont understand metatables at all

umbral spade
#

If u do

#
local module = {}

local rs = game:GetService("RunService")

local Values = {
    ["val1"] = false,
    ["val2"] = true
}
Values.__index = Values

module.mt = {}

setmetatable(module.mt, Values)

print(module.mt)

module.SetValue = function (ValueName, NewValueValue)
    if rs:IsServer() == true then
        print(2)
        module.mt[ValueName] = NewValueValue
    end
end

return module```
#

And then you can acess the .val1 or .val2 from module.mt

solemn turtle
umbral spade
#

If you print module.mt.val1

#

Then it is not nil

solemn turtle
#

oh ok

umbral spade
#

It will look for val1 and find nothing

solemn turtle
#

I tought it is a table with all of it

umbral spade
#

Then it will look for the metatable

#

And it will have __index

#

And then it will find val1 in the Values table

#

And then access that

solemn turtle
#

wait what

#

so if I print the metatable it prints nothing if I print module.mt.val1 it prints false

#

what

#

why

umbral spade
#
local Values = {P = "Hi"}
Values.__index = Values

module.mt = {
  -- hidden: metatable = Values
}

Print(module.mt.P) -- Finds the metatable and then sees the __index metamethod and finds the ["P"] value inside the __index and works```
#

U need to setmetatable on module.mt in that example ops

solemn turtle
#

oh ok...

#

thx

#

but how would I print every thing is the metatable

umbral spade
#

Maybe you can use getmetatable?

#

I am not sure really

#

As for OOP which normally uses a setup like this you store the values in the child table and then functions in the parent table

umbral spade
#

And then you call .new() which creates a NEW child table pointing the the PARENT storing all the functions

#

So then u can just call the functions on the child table

#

You'd have to check but I assume if you made multiple child tables and pointed to the parent with values

#

Then changing the parent value would change it for all of the children

#

I should know that and I assume it does but I've never tried