Metatable insert issue

I was creating a racing game and it loads the player into a metatable, but it’s not going in the table.

playersInRaces[player.UserId] = id
local playermeta = {
	__newindex = function (playersInRace, playerid, raceid)
		local Proccessor = RaceProccessor:GetLocal(raceid)
		local leftConnection = Proccessor.PlayerLeft:Connect(function (playerleftid)
			if playerleftid == playerid then
				rawset(playersInRace, playerid, nil)
			end
		end)
		Proccessor.Reset:Once(function ()
			leftConnection:Disconnect()
			rawset(playersInRace, playerid, nil)
		end)
	end
}
local playersInRaces = {}
setmetatable(playersInRaces, playermeta)

The first is how I added it to the table, the second is the metatable.
When I print the table, even right after I added it, the table is empty.
Any help is appreciated!

playersInRaces[player.UserId] = id

Instead of adding the UserId to the table, it will call the __newindex function. Inside this function you’re only adding the index here

   		if playerleftid == playerid then
   			rawset(playersInRace, playerid, nil)
   		end

So the table would only only upd for player’s who left from what I can tell.
To fix this you could add a rawset near the top of the function like this for example:

__newindex = function (playersInRace, playerid, raceid)
		rawset(playersInRace, playerid, raceid)
		local Proccessor = RaceProccessor:GetLocal(raceid)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.