Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

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.

2 answers

✓ Accepted answer

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!

1 comment

0

Wow, thank you so much! I will try my best to understand whats going on!

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

2 comments

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.

0

I edited it to make it easier to understand

This discussion is preserved as it was. New answers, comments, and votes are closed.