Question about Player:GetFriendsOnline()

So im making a game and for a function i need to use GetFriendsOnline(), but the problem is, im running into a question about GetFriendsOnline()

When it use to get the id of friends, do i need to use for i,v in pairs() or do it already gets the id of all the friends separately

Yes you need for I,v in pairs since it returns a table.

2 Likes

It returns an array of dictionaries so you can use for _, array in ipairs(Player:GetFriendsOnline()) too.

local players = game:GetService("Players")
local localPlayer = players.LocalPlayer

local friendsWith = {} --array of player instances the local player is friends with

local success, result = pcall(function()
	localPlayer:GetFriendsOnline(200)
end)

if success then
	if result then
		for _, friend in ipairs(result) do
			if friend["IsOnline"] then
				if friend["GameId"] == game.JobId then
					local player = players:FindFirstChild(friend["UserName"])
					if player then
						table.insert(friendsWith, player)
					end
				end
			end
		end
	end
end

Here’s a script I wrote yesterday which gets the player’s friends which are currently online in the same server as the player.