A bit of trouble with PlayerAdded

Whats the problem?

So I have a serverscript in ServerScriptService, and it will basically just loop through a players friends and add their UserId to a table called PlayersFriends. But I am scared because what if, I am looping through the players friends and putting them into the table and then running all the checks and stuff I want to do using their UserID’s BUT while doing it, another player joined? Will it just add the player who also joined, friends ID’s to the table? If so, how do I prevent this? (USING A LOCAL SCRIPT IS NOT AN OPTION I CAN DO)

My code

Players.PlayerAdded:Connect(function(player)
	local PlayersFriends = {}
	local success, page = pcall(function() return Players:GetFriendsAsync(player.UserId) end)
	if success then
		repeat
			local info = page:GetCurrentPage()
			for i, friendInfo in pairs(info) do
				table.insert(PlayersFriends, friendInfo.Id)
			end
			if not page.IsFinished then 
				page:AdvanceToNextPageAsync()
			end
		until page.IsFinished
	end
end)

If you need me to explain it better, just ask me I know I can be bad at explaining sometimes.

If another player joined it would simply run that same function again, only the ‘player’ parameter would be the new player that joined and not the previous one. As for code that’s already running when another player joins, it would keep running until it’s finished looping through all the friends.

Okay so just to clarify… I DONT need to worry about other players joining and interfering with the current ID’s in the table and stuff?

Oh wait… do you mean that it will create a new table when the player joins only for that player??

Yes, each time a player is added, a new individual thread is created.

Each time that function is run, a new PlayerFriends table is created for each player. If you’re trying to store each player’s friends table outside the function though, you’ll need another table to hold all the player friends tables.

alright thank you. Ima mark Coderius as the solution since he has responded first, but thank you for clarifying it.

1 Like

Alright and how would I iterate through the second table? Like how would I know if the ID’s in the table belong to a certain players friends

Right, I’ll explain that:

local Players = game:GetService("Players")

local playerFriendsTables = {}

Players.PlayerAdded:Connect(function(player)
        -- This next line is what the player's friends table will be called
        -- in the shared table with all the tables
        local friendsUserId = "Friends_" .. player.UserId

        local PlayersFriends = {}
	local success, page = pcall(function() return Players:GetFriendsAsync(player.UserId) end)
	if success then
		repeat
			local info = page:GetCurrentPage()
			for i, friendInfo in pairs(info) do
				table.insert(PlayersFriends, friendInfo.Id)
			end
			if not page.IsFinished then 
				page:AdvanceToNextPageAsync()
			end
		until page.IsFinished
	end
        
        -- Adding the PlayerFriends table to the second table with all player tables
        playerFriendsTables[friendsUserId] = PlayersFriends
end)

The string variable called “friendsUserId” is how we’ll be able to tell which player’s table is which.

2 Likes

You could store PlayersFriends as an index in another table outside.

local AllFriends = {}

AllFriends[player] = PlayersFriends
2 Likes

absolute legend, one last question, if I were to call a function from players.playeradded, will it also create a new thread for that? or would I have to worry about stuff interfering?

Like call a function inside .PlayerAdded?

In that case, yes it would fire inside that individual thread.

2 Likes