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
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
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, 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
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