Issue with table in ModuleScript

When I try to call :GetPlayerTable(Player) from another script, it doesn’t print {} like I would expect it to, but instead it prints nil

:Initialize() is called when the server starts, and when a Player joins, it sets Players[Player] to an empty table, and I make sure it does by printing it after and it successfully does, yet when I try to retrieve it by using :GetPlayerTable(Player) it doesn’t print {}, but it instead prints nil. The module is correctly required and I do not get any errors.

I’m fairly in-experienced so I might be doing something wrong.


local module = {}

local Players = {}

function module:GetPlayerTable(Player)
	print(Players[Player])
end

function module:Initialize()
	game.Players.PlayerAdded:Connect(function(Player)
		Players[Player] = {}
		print(Players[Player])
	end)
end

return module

There could be a few things causing this:

  1. Are you calling :GetPlayerTable() with a colon?
  2. Do you initialize on the server and then call :GetPlayerTable() on the client?

I call it like this:

require(game.ServerStorage:FindFirstChild("GetPlayerTableModule")):GetPlayerTable(game.Players.RukiaFoot)

And yes, I am doing everything on the server-side.

When I add .Name it still prints nil

`

nevermind what i said, it actually should index the player object itself

lets just wait for other people to help since i am confused with the problem.
You created a table inside a table and index it using the player object, but when you index it by a player object, it prints nil

does the :Initialize function run once the server starts?

edit: also are you testing in roblox studio?

Yes, It’s the first thing that runs. It 100% works, because the print(Players[Player]) prints after the .PlayerAdded() event correctly. The issue is with the :GetPlayerTable() function.

And yes, I’ve tested this in Test Mode, in Play mode, and in-game.

oh, because when play testing in studio, the server and the client load at the same time, so maybe that is the problem, the client loads faster. I just thought

No, the server still loads before the client. Also, do you run the 2nd one after it is initialized?

no, there is a problem about the server and client loading at the same time, causing the .PlayersAdded event to be broken, and that happens to me. This only happens on roblox studio though. To fix this problem, add this after the .PlayersAdded

for i, plr in pairs(game.Players:GetPlayers()) do
	PlayerAddedFunction(plr)
end

This is the code inside a regular “Script” in ServerScriptService.

local Module = require(game.ServerStorage:FindFirstChild("GetPlayerTableModule"))

Module:Initialize()

wait(5)

Module:GetPlayerTable(game.Players.RukiaFoot)

This script and module are the only thing in the game currently, so nothing would be interfering (like lag, lag spikes, fps-drops, etc.)

maybe try this?

local module = {}

local Players = {}

local function PlayerAdded(player)
	Players[Player] = {}
	print(Players[Player])
end

function module:GetPlayerTable(Player)
	print(Players[Player])
end

function module:Initialize()
	game.Players.PlayerAdded:Connect(PlayerAdded)
	for i, plr in pairs(game.Players:GetPlayers()) do
		PlayerAdded(plr)
	end
end

return module

idk what you are doing wrong, I copied the code and ran it and it printed successfully