Hello,
I was looking for a way to make a GUI displaying which of your friends are online in our game. My initial experiment with player:GetFriendsOnline seemed a success, until I found an issue that completely prevents it’s application for this use case.
The script will output “<friendname> has come online!” and “<friendname> has gone offline!” whenever a friend joins or leaves one of the places whose ID is in the ourPlaceIds table.
This works really well, and within 15 seconds after my friend joining or leaving my place, the respective message would come up correctly.
However, when my friend would stay in game but start browsing the website, suddenly my script would output “<friendname> has gone offline!”
When I print the LocationType in that case, it shows “2” which indicates the website.
I have attached a place file below with the code displayed at the bottom of this post installed. Add your places placeId to the table, press ‘play’ and ask your friend to join the place with that placeId / leave / stay in the background but browse the roblox website.
I am aware of Players:GetFriendsAsync(), however it does not provide me with the right information (place ID). An inactivated script to demonstrate this can also be found in this place file under ServerScriptService. Other ideas to achieve my use case are also very welcome, or I will resort to messagingService (I would prefer to not spend it’s usage quota on this). The most ideal solution would be a player.FriendStatusChanged type of event that we could connect to, in my opinion.
getfriendsonline.rbxl (20.2 KB)
The Script
local ourPlaceIds = {"123456789", "4815150781"} -- Enter your place IDs here.
local function isInOurGame(placeId)
for i, ourPlaceId in pairs(ourPlaceIds) do
if ourPlaceId == tostring(placeId) then
return true
end
end
return false
end
local onlineFriends = {}
local function cameOnline(friendName)
print(friendName .. " is online!")
end
local function wentOffline(friendName)
print(friendName .. " went offline!")
end
local function getFriendsOnline()
local success, returnData = pcall( function()
return game.Players.LocalPlayer:GetFriendsOnline()
end)
if not success then
warn("Warning! Cannot retrieve friends list. Error: ", returnData)
return
end
local friendsOnline = returnData
for j, friendData in pairs(friendsOnline) do
local friendId = tostring(friendData["VisitorId"])
local friendName =friendData["UserName"]
local inOurGame = isInOurGame(friendData["PlaceId"])
if( friendData["IsOnline"] ) and inOurGame then
if not onlineFriends[friendId] then
cameOnline(friendName)
onlineFriends[friendId] = true
end
else
if onlineFriends[friendId] then
print("GameId : ", friendData["GameId"])
print("LastOnline : ", friendData["LastOnline"])
print("IsOnline : ", friendData["IsOnline"])
print("PlaceId : ", friendData["PlaceId"])
print("LocationType : ", friendData["LocationType"])
print(friendName)
wentOffline(friendName)
onlineFriends[friendId] = nil
end
end
-- for i, v in pairs(friendData) do
-- print (i, v)
-- end
end
end
wait(1)
while true do
getFriendsOnline()
wait(15)
end