2 metatables are equal to eachother eh... sounds weird, let me explain

Hi guys!
I’ve been struggling with something…
So I got this;
image
This is a module that handles all basically.

Now… this;

local tmp = require(script.profile)
local module = {}
module.__index = module

function module.getplayer(plr)
	return module[plr] or module.new(plr)
end

As you can see, when module.getplayer(plr) get’s called, it returns the metatable stored if its found or, a new one.
Here is .new function()

function module.new(plr)
	assert(plr ~= nil,"plr's nil.")
	local self = setmetatable(tmp,module)
        self.Player = plr
	module[plr] = self
	return self
end

So you wonder… uh, what’s trouble?
this;

local last = nil
		for i,Player in pairs(game.Players:GetPlayers()) do
			local fk = PlayersHandler.getplayer(Player)
			print(last and last == fk or '')
			last = fk
                end

Both print the same, like if they were equals… I’ve tried this on ‘test’ (player 1,player 2) and doesn’t works with 2 players in-game aswell.

This is ‘tmp’
Or the setmetatable(tmp,module)

I really don’t know why they’re both equal, (they’re also returning same values!) no clue why…
When I do print(fk.InRace) they both Return ‘true’ if for example only 1 has it to true… this is really weird, I know.
Think it of as _G.InRace = true
like if fk was _G (it’s not obviously!)

Maybe it’s because tmp isn’t being required for each player? So when you do self.Player = plr it’s overwriting previous values. In other words setmetatable(tmp,module) isn’t returning a new table its just always returning the “original” tmp.

Hey, ty for reply
doing setmetatable(tmp,module) it would be equal like if i did this I think so

setmetatable({
	ToSave = {
		Races = {
			["Winned Races"] = 0;
		};
		Rank = 0;
		Cash = 0;
		XP = 0;
		MapTokens = 0;
		JoinDate = os.date('*t').month..'/'..os.date('*t').day..'/'..os.date('*t').year;
		SelectedCar = 'V5Buggy';
		Vehicles = {
			["V5Buggy"] = {
				EquippedTexture = '';
				VehicleTextures = {};
				ID = https:GenerateGUID();
			};
		};
	};
	FinishedRace = false;
	InRace = false;
},module)

so what I’m trying to say is that it “clones” table

But, I try it out ;p

ok, I can’t explain why this works lol… I though setmetatable(table) – table would be cloned!
Anyway, thanks <3

1 Like