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

Update a printed statement help?

Just a quick question about scripting in general to be more efficient. I was just testing out some code below because I was thinking of making a game with a lobby of players and then I realized that this would not print whenever a new player joins. I know that I could put the for do inside the function, but I was just wondering if there is a way to update something that is printed ever time a change occurs (without using a while loop).

activePlayer = {}

game.Players.PlayerAdded:connect(function(player)
	table.insert(activePlayer, player)
end)

print("Current Players In The Server:")

for i,v in pairs(activePlayer) do
	print(v)
end

I'm aware that this is not a very major question, but thank you for any replies.

1 comment

0

PlayerAdded is a signal that fires every time a new player joins the server. You could try putting the print statement inside there.

2 answers

✓ Accepted answer

So what happened is the script loaded in faster than your player did so the server already went through all the commands before the player.

activePlayer = {}

-- I made this a function so we can call it back as many times as we want, any time someone leaves or joins it will be called so the list prints
-- the upated version of the player list
function refreshPlayerList()
	print("Current Players In The Server:")

	for i,v in pairs(activePlayer) do
	    print(v)
	end
end


game.Players.PlayerAdded:connect(function(player)
    table.insert(activePlayer, player)
	refreshPlayerList()
end)

-- this script takes the players out if they left
game.Players.PlayerRemoved:connect(function(player)
    for i, v in pairs(activePlayer) do
		if v == player then
			table.remove(activePlayer,i)
			refreshPlayerList()
		end
	end
end)

It is usually unwise to do this. For something as trivial as getting the list of players, which will take under a millisecond, there's no point in trying to make it faster.

Your code will almost always be designed better if you make everything immutable, i.e., you never change anything.

The main way to accomplish this is replace global variables with functions as much as possible:

function activePlayers()
	-- ... in this case though, you might as well just use :GetPlayers()
	return game.Players:GetPlayers()
end

However, this can be inefficient because it can only be managed by polling, i.e., repeatedly asking for information.

For example, to show a player list, you need to do something like:

while wait() do
	renderPlayerList( activePlayers() )
end

(Note: the expensive thing isn't calling activePlayers() constantly, it's calling renderPlayerList())

Re-rendering every single frame even though nothing has changed is really wasteful.

However, you don't need complicated code or global variables.

Simply only re-render upon a change that affects it:

function refreshPlayerList()
	renderPlayerList(activePlayers())
end

game.Players.PlayerAdded:connect(refreshPlayerList)
game.Players.PlayerRemoving:connect(refreshPlayerList)
refreshPlayerList()

Even though we do re-do the all of the work for only a small change, because changes are rare enough and refreshPlayerList isn't too expensive, this will be good enough.

Most of the time an architecture like this will suffice when the polling approach does not.

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