local Table = {} local Players = game:GetService("Players") local playercount = 0 local function playerlistprint() print("Current Players:") for i, v in pairs(Table) do print(i.." = "..v) end end Players.PlayerAdded:Connect(function(user) playercount = playercount + 1 table.insert(Table, user.Name) print(Table[playercount].." has joined") playerlistprint() end) Players.PlayerRemoving:Connect(function(user) table.remove(Table, playercount) playercount = playercount - 1 print(Table[playercount]) playerlistprint() end)
The script works I am just wondering what kind of better way I could go about getting all of the current players names into a table and printing them when one leaves or joins. And when a new player joins I print the entire list of players with join order.
The following code looks complicated at first, I've expanded on your example to include a timer.
Your way is good and as you say it works
but the script might get out of sync with too many players leaving and rejoining or even adding the same player to your Table
But with a different index!.
The following code will fix this issues by using the Names of the players as index. For example
table.insert(Table,user.Name)
inserts the user at the end of the table Table = {}
this is great in an ideal world but knowing the real world, things happen, like players leaving before they get registered etc... Or they join for a second then leave due to connection problems. So the following code is my attempt at fixing and coding for these problems. And added a 30-second cool down timer for lag drops.
Brace your self for the code! It's quite complex! :P
local Table = {} local Players = game:GetService("Players") local playercount = 0 --// New Varables for the modifications --// remove the player that left after this many seconds! local RemovePlayerTimer = 30 -- in seconds! --// the Lag table for players that just left local playersLeftTab = {} --// your function as before but modified to shout out who just left and uses your `playercount` Variable to keep track of how many players that are registered. local function playerlistprint() print("Current Players: [Active: ",playercount,"], Just Left: [",#playersLeftTab,"]") for i, v in pairs(Table) do print(i," = ",v) end end --// main PlayersAdded even hookup Players.PlayerAdded:Connect(function(user) --// Increase the index of the actual `playercount` playercount = playercount + 1 --// check to see if the new player thats joined isnt in the Lag table thats just lagged out and just dropped connection. local PlrIndex = table.find(playersLeftTab,user.Name) -- if they are part of that Lag table then remove them from it! if(PlrIndex ~= nil) then table.remove(playersLeftTab,PlrIndex) end --// check to see if the user is already Registered, if not then add them! if(Table[user.Name]== nil) then --Syntax: Table[Player.Name] = Player in game.Players Table[user.Name] = user --// print out to the output that this player has just joined and is new! print(user.Name.." has joined") else --// if they are part of the `Table` table then just print this out print(user.Name.." has Re-Joined") end --// call your custon player print function playerlistprint() end) -- PlayerRemoving event hookup (the fun, tricky bit!) Players.PlayerRemoving:Connect(function(user) -- if the player leaving is part of the Global `Table` (they should be, but sometimes it may bug out) then continue to add them to the Lag table. if(Table[user.Name] ~= nil)then --// add this player at the end of the Lag table, Indexing isn't important in this table since its only holding the name and the time they left in `tick()`'s table.insert(playersLeftTab, { Name = user.Name, Time = RemovePlayerTimer, Ticker = tick() } ) end --// decrease the physical playercount since someone just left! playercount = playercount - 1 --// print this out print(user.Name.." Has Left!") --// update output with the list of players with your custom function playerlistprint() end) --// while do loop for sorting out the lag table and count up to the lag timeout named `RemovePlayerTimer` while true do --// grab the current tick! local NowTick = tick() --// loop through the playersLeftTab table (if this table holds no-one, then this code is just skipped) for k,v in pairs(playersLeftTab) do --// grab the embedded table structure Name, Time and Tick --// the user Name local Name = v.Name --// The Lag timer see (local RemovePlayerTimer = 30 -- in seconds!) above local Time = v.Time --// the tick this player left local Tick = v.Ticker --// Workout the difference between the start and left ticks of each players local DTick = NowTick - Tick --if (DTick is greater or equal to RemovePlayerTimer or Time variable, then remove them from the global Table, table registry. if(DTick >= Time) then Table[Name] = nil --// next remove them from the Lag Table... table.remove(playersLeftTab,k) --// list the remaning players using your custom function playerlistprint() end end -- wait 1 roblox tick wait() end
I've tried to comment what bit does what as plainly as i can. Feel free to ask if you need help! :)
The above code works when put into the ServerScriptService
Service. I've tested it online and it appears to work well. (had two accounts sign in to the game then one leaves then re-joins before the 30 secs. The script successfully detects this and if they leave and let the 30 secs count down the script forgets them!)
Hope this helps!
Simple use PlayerAdded and PlayerRemoving event like this:
game.Players.PlayerAdded:Connect(function(player) print(player.Name.."just joined the game!") end) game.Players.PlayerRemoving:Connect(function(player) print(player.Name.."just left the game!") end)
These 2 events check whenever someone joins or leaves the game. The parameter player is the player who left or joined the game.
Hope it helps