#Get Friend Status

1 messages · Page 1 of 1 (latest)

crystal wren
#
local function getAllFriends(player)
    local userId = player.UserId
    local allFriends = {}

    print("Fetching ...")
    local success, friendsResult = pcall(function()
        return game:GetService("Players"):GetFriendsAsync(userId)
    end)

    if not success then
        warn("Failed " .. tostring(friendsResult))
        return {}
    end

    for _, item in pairs(friendsResult:GetCurrentPage()) do
        if type(item) == "table" and item.Id ~= nil then
            table.insert(allFriends, {
                id = item.Id,
                username = item.Username or "Unknown",
                displayName = item.DisplayName or "Unknown"
            })
        end
    end

    while not friendsResult.IsFinished do
        local success, nextPageResult = pcall(function()
            return friendsResult:AdvanceToNextPageAsync()
        end)

        if not success then
            warn("Failed " .. tostring(nextPageResult))
            break
        end

        for _, item in pairs(friendsResult:GetCurrentPage()) do
            if type(item) == "table" and item.Id ~= nil then
                table.insert(allFriends, {
                    id = item.Id,
                    username = item.Username or "Unknown",
                    displayName = item.DisplayName or "Unknown"
                })
            end
        end
    end

    print("Found " .. #allFriends .. " friends.")
    return allFriends
end

local player = game.Players.LocalPlayer
if player then
    local friends = getAllFriends(player)
else
    warn("player not found")
end
``` i would like to get the friends status(Offline/In Game/Website) but i can't manage to make it could anyone help me rq please thx this just get all the friend from a player between
#

Get Friend Status

void osprey
#

cant do it from local scripts or client side i believe

#

pretty sure theres a getFriendsAsync method

#

ive seen ppl use

#

@crystal wren try this

crystal wren
#

Could you gave me an example please ?

void osprey
#

one sec

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

local function getAllFriendsWithStatus(player)
    local userId = player.UserId
    local allFriends = {}

    print("Fetching friends for " .. player.Name)
    local success, friendsResult = pcall(function()
        return Players:GetFriendsAsync(userId)
    end)

    if not success then
        warn("Failed to get friends: " .. tostring(friendsResult))
        return {}
    end

    local function convertPresence(presenceType)
        if presenceType == 0 then
            return "Offline"
        elseif presenceType == 1 then
            return "Online"
        elseif presenceType == 2 then
            return "In Game"
        elseif presenceType == 3 then
            return "In Studio"
        else
            return "Unknown"
        end
    end

    repeat
        for _, item in ipairs(friendsResult:GetCurrentPage()) do
            if item.Id then
                table.insert(allFriends, {
                    id = item.Id,
                    username = item.Username or "Unknown",
                    displayName = item.DisplayName or "Unknown",
                    status = convertPresence(item.PresenceType or 0)
                })
            end
        end
    until not friendsResult:AdvanceToNextPageAsync()

    print("Found " .. #allFriends .. " friends.")
    return allFriends
end

Players.PlayerAdded:Connect(function(player)
    local friends = getAllFriendsWithStatus(player)

    for _, friend in ipairs(friends) do
        print(friend.username .. " - " .. friend.status)
    end
end)

then this should work for you

crystal wren
#

Thats really stoping me rn i can’t mange to make it work

void osprey
#

ServerScript

crystal wren
#

No local ?

void osprey
#

dont believe so

#

what are you trying to do

#

have it show in ui?

crystal wren
#

Or do i need to the do like local —> remote function —>Serverscript—>Local

void osprey
#

yess

#

If you ever do want to show this info to the player's UI, you can use a RemoteFunction or RemoteEvent to send it from the server to a LocalScript

crystal wren
void osprey
#

aaahh i see

smoky flameBOT
#

studio** You are now Level 1! **studio

crystal wren
#

I manage to make everything working for now except this friend shit

void osprey
#

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

local GetFriendStatuses = ReplicatedStorage:WaitForChild("GetFriendStatuses")

local function fetchAndPrintFriendStatuses()
local player = Players.LocalPlayer
local friends = GetFriendStatuses:InvokeServer()

for _, friend in ipairs(friends) do
    print(friend.username .. " - " .. friend.status)
end

end

-- Example call on startup
fetchAndPrintFriendStatuses()

#

paste that into local script

#

this is example

crystal wren
#

Alright I am gonna try tomorow

void osprey
#

put remote function in replicated storage

#

then you should get it printed out in output window

crystal wren
#

Working on This rn

void osprey
#

nice