Advertisement
BenTibnam

Updating Player List in Roblox

Mar 8th, 2022
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.08 KB | None | 0 0
  1. local playerService = game:GetService("Players")
  2. local bindableFunction = game.ReplicatedStorage.GetPlayers
  3.  
  4. local players = {}
  5.  
  6. -- used for debugging tools
  7. local function listPlayers()
  8.     for i, player in pairs(players) do
  9.         print(player)
  10.     end
  11. end
  12.  
  13. -- adds player to the player list
  14. playerService.PlayerAdded:Connect(function(player)
  15.     local playerTableListLength = #players
  16.     local newPlayerPosition = playerTableListLength + 1
  17.     players[newPlayerPosition] = player.Name
  18.    
  19. end)
  20.  
  21.  
  22. -- removes player from the player list
  23. playerService.PlayerRemoving:Connect(function(player)
  24.     local playerName = player.Name
  25.     local leavingPlayerPosition = -1
  26.    
  27.     -- updating list
  28.     local playerListBuffer = {}
  29.     local currentIndex = 1
  30.     for i, player in pairs(players) do
  31.         if player == playerName then
  32.             continue
  33.         end
  34.        
  35.         playerListBuffer[currentIndex] = player
  36.         currentIndex += 1
  37.     end
  38.    
  39.     -- assigning buffer to the player list
  40.     players = playerListBuffer 
  41.    
  42. end)
  43.  
  44. local function getPlayerList()
  45.     return players
  46. end
  47.  
  48. bindableFunction.OnInvoke = function()
  49.     return getPlayerList()
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement