How would i get all online friends from the player?

I’m trying to make a friends list gui for my game but I can’t figure out how to use GetFriendsAsync(), i’ve looked on other threads but i cant seem to figure it out

For the gui I’m wanting, the friend UserId, DisplayName and Username

script: (LocalScript)

local success, result = pcall(Player.GetFriendsOnline, Player, 10)

while wait(2.5) do
	if success then
		for _, friend in pairs(result) do
			
			print(friend.Name)
			local Clone = AllFriendsListTEMPLATE:Clone()
			Clone.Parent = AllFriendsList
			Clone.Name = Player.Name.."FRIEND"..friend.Name -- Line with error
			Clone.DisplayName.Text = friend.DisplayName
			Clone.UsernameTitle.Text = friend.Name.."#"..friend.UserId
		end
	else
		warn("Failed to get online players: " .. result)
	end
end

I took the code from Player | Documentation - Roblox Creator Hub

Error:

attempt to concatenate string with nil

All help is appreciated :smile:

1 Like

The friend dictionary returned has no property Name, the properties you’re looking for are UserName and DisplayName. I made you a type to make your life easier:

type friend = {DisplayName: string, GameId: string?, IsOnline: boolean, LastLocation: string?, 
	LastOnline: string, LocationType: number, PlaceId: number?, UserName: string, VisitorId: number}

Add that on top of your script and type : {friend} after result to denote it returns an array of friends.

That way those properties will auto-complete down below when you type friend..

1 Like

Thank you!! works wonderfully :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.