I am making a votekick system, but got stuck here.
I have a table VotekickInfo that stores the player votes, using players as keys.
However, when trying to get the Name property of the Player, it prints nil.
Here is the relevant code and outputs:
--## Script
local VotekickInfoChanged --## It's a remote event
local VotekickInfo = {}
local function PlayerJoined(Player)
--## Connected to Players.PlayerAdded
VotekickInfo[Player] = {}
VotekickInfoChanged:FireAllClients(VotekickInfo)
end
--## LocalScript
local VotekickInfo = {}
local function OnVotekickInfoChanged(ServerVotekickInfo)
--## Connected to the remote
VotekickInfo = ServerVotekickInfo
for Player, Votes in pairs(VotekickInfo) do
print(VotekickInfo)
print(Player)
print(Player.Name)
print(Player.Parent)
end
end
--## After joining game
{
["(Windsonnes)"] = {}
}
<Instance> (Windsonnes)
nil
nil
--## Script
local VotekickInfoChanged --## It's a remote event
local VotekickInfo = {}
local function PlayerJoined(Player)
--## Connected to Players.PlayerAdded
VotekickInfo[Player.Name] = {}
VotekickInfoChanged:FireAllClients(VotekickInfo)
end
--## LocalScript
local VotekickInfo = {}
local function OnVotekickInfoChanged(ServerVotekickInfo)
--## Connected to the remote
VotekickInfo = ServerVotekickInfo
for PlayerName, Votes in pairs(VotekickInfo) do
local Player = game.Players:FindFirstChild(PlayerName)
if Player then
print(VotekickInfo)
print(PlayerName)
print(Player.Name)
print(Player.Parent)
end
end
end
--## After joining game
{
["Windsonnes"] = {}
}
Windsonnes
Windsonnes
Players
The issue was that the Player object was being used as the key for the VotekickInfo table, however the Player object does not have a Name property so it was returning nil. To solve the issue, the Player’s name should be used as the key for the VotekickInfo table instead. In the OnVotekickInfoChanged function, you can use the game.Players:FindFirstChild() function to get the Player object from the PlayerName key, which will allow you to access the Name property.