Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I am very new to scripting and wondering if I can get some help making my code better?

Asked by 3 years ago
Edited 3 years ago
01local Table = {}
02local Players = game:GetService("Players")
03local playercount = 0
04 
05local function playerlistprint()
06    print("Current Players:")
07    for i, v in pairs(Table) do
08        print(i.." = "..v)
09    end
10end
11Players.PlayerAdded:Connect(function(user)
12    playercount = playercount + 1
13    table.insert(Table, user.Name)
14    print(Table[playercount].." has joined")
15    playerlistprint()
View all 23 lines...

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.

0
I skimmed Gazza's post and I think he might've mentioned this but I'd use a dictionary. Table[plr.Name] = true...... print(Table["greatneil80"]).... true, it'd save a lot of time and effort. greatneil80 2647 — 3y

2 answers

Log in to vote
1
Answered by
TGazza 1336 Moderation Voter
3 years ago

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

1table.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

001local Table = {}
002local Players = game:GetService("Players")
003local playercount = 0
004 
005--// New Varables for the modifications
006--// remove the player that left after this many seconds!
007local RemovePlayerTimer = 30 -- in seconds!
008 
009--// the Lag table for players that just left
010local playersLeftTab = {}
011 
012--// 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.
013 
014local function playerlistprint()
015    print("Current Players: [Active: ",playercount,"], Just Left: [",#playersLeftTab,"]")
View all 104 lines...

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!

0
Wow, thank you so much! I will try my best to understand whats going on! tightanfall 110 — 3y
Ad
Log in to vote
0
Answered by
Soban06 410 Moderation Voter
3 years ago

Simple use PlayerAdded and PlayerRemoving event like this:

1game.Players.PlayerAdded:Connect(function(player)
2    print(player.Name.."just joined the game!")
3end)
4 
5game.Players.PlayerRemoving:Connect(function(player)
6    print(player.Name.."just left the game!")
7end)

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

0
Im not sure if you read the full code (no worries if you didnt). But what's happening is I am compiling all of the names of the players into a table and printing all of their names and join order. Shown at line 14. tightanfall 110 — 3y
0
I edited it to make it easier to understand tightanfall 110 — 3y

Answer this question