Local variable question

game.Players.PlayerAdded:Connect(function(player)
	
local Table1 = {
	[player] ={}

what is happening here? is Table1 being overwritten everytime a player is added, or is it making a new table each time

It makes a new table every time the listener is called. Eventually it gets garbage collected after since there is no more references to it.

lets say i do this instead

game.Players.PlayerAdded:Connect(function(player)
	
local Table1 = {
	[player] ={dog = "lol"}

will it get garbage collected anymore?

There are no more external references to the table so yes it still will.

game.Players.PlayerAdded:Connect(function(player)
	
local Table1 = {
	[player] ={dog = "lol"}

Climb.OnServerEvent:Connect(function(player,key)
print(Table1[player].dog)

will this prevent garbage collection?

assumming that there are 2 players and player 1 fires the event

yes that prevents garbage collection, the variable Table1 is available as an upvalue in the OnServerEvent listener

local Table1 = {}

game.Players.PlayerAdded:Connect(fuction(player)
Table1[player] = {...}
...

so in lua there are closures (i really don’t like lua documentation)
even though that piece of code gets run from some unknown place in the future when a player joins. It can still access Table1 due to the reference being available at definition.

why does this not print

local Climb = game:GetService("ReplicatedStorage"):WaitForChild("Climb")
game.Players.PlayerAdded:Connect(function(player)
	
local Table1 = {
[player] ={dog = "lol"}}
		
Climb.OnServerEvent:Connect(function(player,key)
		print(Table1[player].dog)
	end)
end)

this got fired from player2

Every user will have a local table for them

User 1:

game.Players.PlayerAdded:Connect(function(player)
	
local Table1 = {
	['greatneil80'] ={}

User 2:

game.Players.PlayerAdded:Connect(function(player)
	
local Table1 = {
	['SomeoneElse'] ={}

It is inside the PlayerAdded so basically only the player can see that section so yes, the table is being created each time, it couldn’t be overwritten unless the player had the same name as another player and if the local Table1 was behind the game.Players.PlayerAdded

local Table1 = {
	['SomeoneElse'] ={}
}
game.Players.PlayerAdded:Connect(function(player)
    Table1[player] = {}
end)

Table will end up with many players inside the table rather than each having their separate table.

Linked tables are pretty useful.

1 Like

Alright first lets clean up the code because lets be clear, this is pretty… uhm well yea, lets just make it easier for us to read.


local module = {}
module.PlayerTable = {}

-- OnClimeEvent
local OnClimbEvent = game:GetService("ReplicatedStorage"):WaitForChild("OnClimeEvent")
-- [[
   -- This function is for when a player joins creating a quick ref without having
   --to call for all players, as well as lets me do open checks later
]]
local function OnPlayerConnect_Setup(player)
     -- Here we are storing the player by their USER id within this module so we 
     -- can reference it later or pass it elsewhere
     module.PlayerTable[player.UserId] = player
end
game.Players.PlayerAdded:Connect(OnPlayerConnect_Setup)

--[[
   -- When the player requests or begins to climb I fire an event and want
  -- to know their climbing
]]	
local function playerClimbingEvent(player, key)
     print(module.PlayerTable[player.UserId])
end		
OnClimbEvent.OnServerEvent:Connect(playerClimbingEvent)		

return module

Alright now our code is cleaner and we can READ this for once!! Lol a much better pattern to understand what your trying to do and some better naming to help us understand what your trying to do and what variables mean what

Now the reason your table was being over written is 2 fold. If you took it out of the context of that function you were connecting and then stored it, would have been better but unless its in a module its not going to be accessable anywhere else. Right now, if you require this module code it would set everything up with your connections and then you could require this in other places and access the player reference where ever you want too.

There are some better patterns here that make that part easier, but at the end of the day atm you have a pure function ( a function with zero external dependancies, so every time you call it that local varable is complety blown away. and you DO NOT want to try to load a random object into it to keep it around from garbage collect with ZERO direct reference to it. MEMORY LEAK in bound for sure. Just store it as module data.