I'm working on a story game, and right now I'm working on the lobby system, where the players join the lobby to get teleported and all that stuff. I have a GUI-based layout where you click buttons to get into each lobby (not physically walking into one like most other story games). My system is that each player is assigned a value in a dictionary, and the dictionary updates when players join or leave. However whenever I loop through the table to check an empty value to assign the player to, it doesn't seem to do that. No errors or anything, just...nothing.
Here is my code:
01 | local players = { |
02 | Player 1 = nil ; |
03 | Player 2 = nil ; |
04 | Player 3 = nil ; |
05 | Player 4 = nil ; |
06 | Player 5 = nil ; |
07 | Player 6 = nil ; |
08 | Player 7 = nil ; |
09 | Player 8 = nil ; |
10 | } |
11 |
12 | game.ReplicatedStorage.Chapter 1. AddToLobby.OnServerEvent:Connect( function (p) |
13 | print ( "nsdn" ) |
14 | for i, v in ipairs (players) do |
15 | print ( "yee" ) |
16 | wait() |
17 | end |
18 | end ) |
How do I fix this?
Setting a value to nil in a dictionary is the same as removing it entirely, having it not exist, so basically you can visualize:
01 | local players = { |
02 | Player 1 = nil ; |
03 | Player 2 = nil ; |
04 | Player 3 = nil ; |
05 | Player 4 = nil ; |
06 | Player 5 = nil ; |
07 | Player 6 = nil ; |
08 | Player 7 = nil ; |
09 | Player 8 = nil ; |
10 | } |
as this:
1 | local players = { } |
Another thing to remember is that ipairs requires order while pairs doesn't. Since a dictionary has no order because like.. they are words... in a table, it won't print.
01 | local players = { |
02 | Player 1 = "" |
03 | Player 2 = "" |
04 | Player 3 = "" |
05 | Player 4 = "" |
06 | Player 5 = "" |
07 | Player 6 = "" |
08 | Player 7 = "" |
09 | Player 8 = "" |
10 | } |
11 | game.ReplicatedStorage.Chapter 1. AddToLobby.OnServerEvent:Connect( function (p) |
12 | print ( "nsdn" ) |
13 | for i, v in pairs (players) do |
14 | print ( "yee" ) |
15 | wait() |
16 | end |
17 | end ) |
You can use empty space as an alternative or even a boolean, whatever suits you.