Hi again developers!, Welcome to this new part of API tutorials!, this time we will see some functions and endpoints related to friends and followers
I want to emphasize that I wont do as many tutorials as it seems, but only those that have many topics on the forum
Today we will see:
- Check friendship between players
- Player friend online status
- Follower and Friends Count
- Check if a player is following another player
Lets Start!
IsFriendsWith(): Player is friend with another
The parameter is the UserId of a player [weâll see if the joined player is his friend]
--Server
game.Players.PlayerAdded:Connect(function(player)
if player:IsFriendsWith(1) then -- Is he friend of Roblox? No way!
print(player.Name .. " is friend of Roblox!")
end
end)
.
Player Friends Count: Also the name of each of his friends
Dont get me wrong but many topics have their answers in the Roblox API, you just have to understand and make some similar code
local Players = game:GetService("Players")
local PlayerName = "PLAYERNAME" -- player username
local usernames = {}
local function iterPageItems(pages)
return coroutine.wrap(function()
local pagenum = 1
while true do
for _, item in ipairs(pages:GetCurrentPage()) do
coroutine.yield(item, pagenum)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
pagenum = pagenum + 1
end
end)
end
local userId = Players:GetUserIdFromNameAsync(PlayerName)
local friendPages = Players:GetFriendsAsync(userId)
for item, pageNo in iterPageItems(friendPages) do
table.insert(usernames, item.Username)
end
--Number of Friends
print(#usernames)
--Who are his friends
print("Friends of " .. PlayerName .. ": " .. table.concat(usernames, ", "))
.
Friend Online Status: This means checking if the player friends are online, on mobile or playing a random game.
--Local
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local onlineFriends = player:GetFriendsOnline(200) -- Max: 200
local DictStatus = { -- A dictionary that I made
[0] = "is online on mobile",
[1] = "is playing on mobile",
[2] = "is online",
[3] = "is on Roblox studio",
[4] = "is playing a game",
[5] = "is on Xbox",
[6] = "is on TeamCreate"
}
for i, friend in pairs(onlineFriends) do
print(friend.Username, DictStatus[friend.LocationType], "| seen at: "..friend.LastLocation)
print(friend.UserName)
-- you can also get the GameId that he is playing with friend.PlaceId
-- and his last online with friend.LastOnline
end
To understand the dictionary: If âfriend.LocationTypeâ gives me the number 3 [because it only gives numbers], then the dictionary gives me âis on Roblox studioâ because in there, the number 3 means that
SocialService and FollowUserId: Here we will see how to prompt a gui like this to invite friends, friends and more friendsâŚ
--LocalScript inside button
local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent
button.MouseButton1Click:Connect(function()
local canInvite = SocialService:CanSendGameInviteAsync(player)
if canInvite then --check if the function is possible
SocialService:PromptGameInvite(player) --prompt
end
end)
The âFollowUserIdâ part is not very necessary, see if a user has joined the game of a friend [who is in the game] this works thanks to this button
--Local
local player = game.Players.LocalPlayer
game.Players.PlayerAdded:Connect(function(newplayer)
if newplayer.FollowUserId == player.UserId then
print(newplayer.Name.." has joined your game!")
end
end)
In this case, if I press the button then I am the user that follows the user in the game, [in other words, I would be the ânewplayerâ and the notification is for my friend ingame]
Warning:
From here on we are going to need HttpService which means: we need to activate the API and some permissions, all of the below functions wont work without the following requirements
.
Player Follower Count:
Roproxy wont only help us for this, but will be our travel companion to all Roblox endpoints, since roblox doesnt allow access
Many endpoints arent âGETâ method, Roproxy cannot get out of that limit so I will only explain some of these
local HttpService = game:GetService("HttpService")
local UserId = 1
local Table = "https://friends.roproxy.com/v1/users/"..UserId.."/followers/count"
local response = HttpService:RequestAsync({
Url = Table,
Method = "GET"
});
if response.Success then
local TableBody = HttpService:JSONDecode(response.Body)
print("Player has "..TableBody.count.." followers")
end
.
Check if player is following another player: Coding this cost me, there are some topics that ask that, but they have more controversy than answers For it to work, the player has to follow less than 100 players.
local HttpService = game:GetService("HttpService")
local Follower = 1159951169
local Followed = 1
local Table = "https://friends.roproxy.com/v1/users/"..Follower.."/followings?sortOrder=Asc&limit=100"
local response = HttpService:RequestAsync({
Url = Table,
Method = "GET"
});
if response.Success then
local TableBody = HttpService:JSONDecode(response.Body)
for index, v in pairs(TableBody.data) do
if v.id == Followed then
print("Player is following Roblox")
end
end
end
Please dont use this for profit like giving rewards for following the creator or things like that, I already gave the notice so Im not responsible for anything
Anything extra you want to see about the Friends API can be found here. Ive made some more tutorials on specific APIs, feel free to explore