I want to pass dictionaries to the client but it errors when I try and search it up. I’m not really familiar with these things since I never found a use for them until now. It errors on the client script that playername is missing or nil
Server Script
-- Adding players to the dictionary
local num = nil
local role = nil
repeat
num = math.random(1,5)
role = GoodRoles[num]
until role ~= nil
GoodTaken[num] = plr.Name
table.remove(GoodRoles,num,role)
AddGoodTeam(plr,PlrsRandom,role)
-- Firing the Event
RoundEnd:FireAllClients(winner,solo,GoodTaken,DeadGood,EvilTaken,DeadEvil,timeleft,PlrsRandom)
Client Script
-- Changing stuff based on the Dictionaries
RoundEnd.OnClientEvent:Connect(function(winner, solo, GoodTaken, DeadGood, EvilTaken, DeadEvil, Timeleft, Players)
-- Round Stats
RoundStats.Time.Text = RoundStats.Time.Text.." "..convert(240-Timeleft)
local Dead = #DeadGood + #DeadEvil
RoundStats.Alive.Text = RoundStats.Alive.Text.." "..#Players - Dead
RoundStats.Dead.Text = RoundStats.Dead.Text.." "..Dead
--
-- Humans
for i = 1,5 do
local playername = GoodTaken[i]
local plr = game.Players:FindFirstChild(playername)
if plr then
print("Found Player")
local tag = nil
if i == 1 then
tag = Humans.Hunter
elseif i == 2 then
tag = Humans.Wizard
elseif i == 3 then
tag = Humans.SWAT
elseif i == 4 then
tag = Humans.Medic
elseif i == 5 then
tag = Humans.Technician
end
tag.PlrName.Text = plr.Name
tag.ImageLabel.Image = game.Players:GetUserThumbnailAsync(plr.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
local deadindex = table.find(DeadGood,plr.Name,1)
if deadindex then
tag.Dead.Visible = true
else
if solo == true then
tag.PlrName.TextColor3 = Color3.new(1, 1, 0)
end
end
end
end
--
-- Creatures
for i = 1,5 do
local playername = EvilTaken[i]
local plr = game.Players:FindFirstChild(playername)
if plr then
print("Found Player")
local tag = nil
if i == 1 then
tag = Creatures.Vampire
elseif i == 2 then
tag = Creatures.Zombie
elseif i == 3 then
tag = Creatures.Werewolf
elseif i == 4 then
tag = Creatures.Imp
elseif i == 5 then
tag = Creatures.Ghost
end
tag.PlrName.Text = plr.Name
tag.ImageLabel.Image = game.Players:GetUserThumbnailAsync(plr.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
local deadindex = table.find(DeadEvil,plr.Name,1)
if deadindex then
tag.Dead.Visible = true
else
if solo == true then
tag.PlrName.TextColor3 = Color3.new(1, 1, 0)
end
end
end
end
--
end)
This seems to only put one player name into GoodTaken:
This assumes that there are 5 names in GoodTaken:
So I think it boils down to:
account for there being < 5 player names on the client side
fix a bug on your server side
P.S.
You could just send a list of players directly rather than using the names and then trying to find the player again on the client. There is also “new” syntax for for loops now e.g.
local players: {Player} = {Player1, Player2, ...}
for i, player in players do
print(i, player.Name)
end
--Output:
-- 1 Player1
-- 2 Player2
-- ...
I’m sorry I didn’t add this part because I didn’t think it was important for the whole dictionary thing, I have accounted for all players
There always aren’t gonna be 5 players in the game anyways so the client scripts check if the player is even real, It’s just that it won’t check it because playername is missing or nil (Like I said originally)
Server script [Whole Dictionary Adding Part]
for i = 1,#PlrsRandom do
local index = table.find(PlrsRandom,Players[i],1)
local playername = PlrsRandom[index]
local plr = game.Players:FindFirstChild(playername)
if i%2 == 0 then
if #GoodRoles > 0 then
local num = nil
local role = nil
repeat
num = math.random(1,5)
role = GoodRoles[num]
until role ~= nil
GoodTaken[num] = plr.Name
table.remove(GoodRoles,num,role)
AddGoodTeam(plr,PlrsRandom,role)
else
local num = nil
local role = nil
repeat
num = math.random(1,5)
role = EvilRoles[num]
until role ~= nil
GoodTaken[num] = plr.Name
table.remove(EvilRoles,num,role)
AddEvilTeam(plr,PlrsRandom,role)
end
elseif i%2 == 1 then
if #EvilRoles > 0 then
local num = nil
local role = nil
repeat
num = math.random(1,5)
role = EvilRoles[num]
until role ~= nil
GoodTaken[num] = plr.Name
table.remove(EvilRoles,num,role)
AddEvilTeam(plr,PlrsRandom,role)
else
local num = nil
local role = nil
repeat
num = math.random(1,5)
role = GoodRoles[num]
until role ~= nil
GoodTaken[num] = plr.Name
table.remove(GoodRoles,num,role)
AddGoodTeam(plr,PlrsRandom,role)
end
end
end
There always aren’t gonna be 5 players in the game anyways so the client scripts check if the player is even real, It’s just that it won’t check it because playername is missing or nil (Like I said originally)
Yes, but the loop will error if it will encounter a nil entry.
for i = 1,5 do
local playername = GoodTaken[i]
local plr = game.Players:FindFirstChild(playername) -- this line will error, if the playername is nil
To correct this use:
local playername = GoodTaken[i]
if playername == nil then continue end -- ignore this entry and continue with the next iteration of this loop
local plr = game.Players:FindFirstChild(playername)
Edit: Alternatively you may use pairs loop, that will only iterate through existing entries:
for _, playername in pairs(GoodTaken) do
local plr = game.Players:FindFirstChild(playername)
I think the minimum you’d need to do to get it working is this:
for i = 1,5 do
local playername = GoodTaken[i]
if not playername then break end -- Guard against playername being nil
local plr = game.Players:FindFirstChild(playername)
Which is equivalent to doing this really:
for _, playername in GoodTaken do
local plr = game.Players:FindFirstChild(playername)
What I originally thought was FindFirstChild would just search it up no matter what so I was checking the nil that way, I still have a long way to go apparently. Thank you