So I'm trying to apply/test my beginner knowledge and I appear to have gotten stuck. What I'm trying to explore is the game.Players:GetPlayers()
method/function.
I'm guessing that if I call that function after a player is leaving, it will return a table without the player that left? I'm assuming I'm wrong because it doesn't seem to be working. If that's the case, then I'd like some help on how to do exactly that.
Furthermore, an error prompts when the PlayerRemoving event is called, simply saying "An error has occurred" and doesn't print the table.
01 | playerservice = game:GetService( "Players" ) |
02 |
03 | function print_table(tab) -- I want to print out what's in the table |
04 | for i,v in ipairs (tab) do |
05 | local val = tostring (v) |
06 | print ( "i = " ..i.. " v= " ..val) |
07 | end |
08 | end |
09 |
10 | playerservice.PlayerAdded:connect( function (plr) |
11 | print_table(playerservice:GetPlayers()) |
12 | end ) |
13 | playerservice.PlayerRemoving:connect( function (plr) |
14 | print_table(playerservice:GetPlayers()) |
15 | end ) |
I'm not sure if your way would work or if there was a way to make it work, but what I do is this. Idk why really you are using a service called "Players" being as you could just do game.Players to access it.
01 | plr = { } -- The table for the players |
02 | p = game.Players:GetPlayers() -- This is basically just used so you can get a list of all of the players |
03 |
04 | for i = 1 ,#p do |
05 | table.insert(plr, p [ i ] ) -- Takes the list of all people and inserts them into the table called "plr" |
06 | end |
07 |
08 | function getNumber(obj) -- this function will be used to get who left |
09 | for i = 1 ,#plr do -- go through all the players |
10 | if plr [ i ] = = obj then -- if the person in the list is the one you are looking for |
11 | return i -- return that certain person |
12 | end |
13 | end |
14 | end |
15 |
Then you could use the list of players for different things such as making custom player lists, which I actually used this for.
If this helped please remember to mark it as answered, if not then please message me or comment so I can know what I could do better for you!