#metatables
1 messages · Page 1 of 1 (latest)
Main Script:
local ps = game:GetService("Players")
local test = require(script.test)
local super_test = require(script.super_test)
local testing_area = {
}
function outside_scope(player)
testing_area[tostring(player.UserId)]:changecolor() -- Changes color but to green
print(testing_area)
end
function player_added(player)
local var = test.new(player) -- method 1, color is red
testing_area[tostring(player.UserId)] = var
var:changecolor() -- Changes to blue
local var_2 = super_test.new(player, testing_area[tostring(player.UserId)]) -- method 2
var_2:changecolor() -- Changes to green
outside_scope(player)
end
ps.PlayerAdded:Connect(player_added)
test script:
local test = {}
test.__index = test
local data_set = {
default = "test";
color = "red"
}
function test.new(player, self)
if self then setmetatable(self, test) end
local self = setmetatable({}, test)
self.name = player.Name
self.data_set = data_set
return self
end
function test:changecolor()
self.color = "blue"
return self
end
return test
super test script:
local supertest = {}
supertest.__index = supertest
function supertest:changecolor()
self.color = "green"
return self
end
function supertest.new(player, data, self)
if self then return setmetatable(self, supertest) end
local self = setmetatable(data, supertest)
return self
end
return supertest
main question is why testing_area[tostring(player.UserId)]:changecolor() is changing to green and not blue?
trying to understand why it was overwritten with super_test even though i passed it into super_test in the second parameter
they are the same table in this case
well no
but you make them into the and object of the same class
a table can only have 1 metatable
oh
meaning here you override the prev metadata
WOW