Can you detect if someone friends another user ingame?

Is it possible to detect if a user becomes friends with another while both users are in the same server? I want to create a badge that is rewarded when two users become friends, but wouldn’t know if there is any sort of way to manipulate the get friends API. Any help would be appreciated!

5 Likes

I think you can create While loop, and inside it for loop and check every player if its friends with other

example:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(mainPlr)
	while mainPlr do
		for _, plr in pairs(Players:GetPlayers()) do
			if mainPlr:IsFriendsWith(plr.UserId) then
				--award badge
			end
		end
		task.wait(5)
	end
end)
2 Likes

roblox does not provide a direct API to detect when two users become friends while in the same server. The GetFriendsAsync method in Roblox is used to fetch a player’s friends list, but it does not have any events or hooks that can notify the script when a change in the friends list occurs. However, you can implement a workaround to achieve your goal.

i made this for you, you obviously have to modify the badge award stuff, and replace it with your stuff, read the script notes that i put on it, you have to put your things there, or it will not work:

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local FRIEND_BADGE_ID = 123456 -- Replace with your badge ID
local CHECK_INTERVAL = 10 -- Time in seconds between checks
local playerFriends = {} -- Table to store friends lists

-- Function to fetch a player's friends
local function getFriends(userId)
    local success, result = pcall(function()
        return Players:GetFriendsAsync(userId)
    end)
    if success then
        local friends = {}
        for _, friend in ipairs(result:GetCurrentPage()) do
            table.insert(friends, friend.Id)
        end
        return friends
    else
        warn("Failed to fetch friends for userId " .. userId .. ": " .. tostring(result))
        return {}
    end
end

-- Function to award the badge
local function awardBadge(player)
    local success, result = pcall(function()
        return BadgeService:AwardBadge(player.UserId, FRIEND_BADGE_ID)
    end)
    if success then
        print("Badge awarded to " .. player.Name)
    else
        warn("Failed to award badge: " .. tostring(result))
    end
end

-- Periodically check for new friendships
task.spawn(function()
    while true do
        for _, player in pairs(Players:GetPlayers()) do
            local currentFriends = getFriends(player.UserId)
            local previousFriends = playerFriends[player.UserId] or {}

            -- Compare the lists for new friends
            for _, friendId in ipairs(currentFriends) do
                if not table.find(previousFriends, friendId) then
                    local friendPlayer = Players:GetPlayerByUserId(friendId)
                    if friendPlayer then
                        print(player.Name .. " became friends with " .. friendPlayer.Name)
                        awardBadge(player)
                        awardBadge(friendPlayer)
                    end
                end
            end

            -- Update the stored friends list
            playerFriends[player.UserId] = currentFriends
        end
        wait(CHECK_INTERVAL)
    end
end)

-- Cleanup when players leave
Players.PlayerRemoving:Connect(function(player)
    playerFriends[player.UserId] = nil
end)

3 Likes