So, what I’m trying to achieve here is I’m trying to detect if your friend is playing a Roblox game, mine specifically because I’m making a hub/lobby where you can join your friends IF they’re in your experience, but I’m not sure how to do this. So, in short is there a way to make it so I can see if my friend is in my game and then be able to follow him all through script within the hub/lobby I’m creating?
First, create a GUI button in your game. You can do this in Roblox Studio by inserting a ScreenGui into the StarterGui and then adding a TextButton inside the ScreenGui.
Step 2: LocalScript for the Button
Detecting Friends and Showing in GUI:
local player = game.Players.LocalPlayer
local gui = player.PlayerGui:WaitForChild("ScreenGui")
local button = gui:WaitForChild("Button") -- Replace with your button's name
local friendsList = {} -- To store friend info
local function updateFriendsList()
friendsList = {} -- Reset list
local success, friends = pcall(function()
return player:GetFriendsOnline(50) -- Getting up to 50 online friends
end)
if success then
for _, friend in pairs(friends) do
if friend.IsOnline and friend.PlaceId == game.PlaceId then
-- Add friend info to friendsList, assuming they're in the same game
table.insert(friendsList, friend)
end
end
else
warn("Failed to get friends list")
end
end
-- Call this function periodically or via some event to update the list
updateFriendsList()
Creating GUI for Friend List:
You would need to create a GUI element to list these friends. This part of the script assumes you have a Frame or ScrollingFrame to list the friends and a TextButton template to clone for each friend.
local friendsFrame = gui:WaitForChild("FriendsFrame") -- Replace with your frame's name
local function displayFriends()
for _, friend in pairs(friendsList) do
local friendButton = button:Clone()
friendButton.Parent = friendsFrame
friendButton.Text = friend.Username
friendButton.Visible = true
-- Teleport to friend's game on click
friendButton.MouseButton1Click:Connect(function()
game:GetService("TeleportService"):Teleport(game.PlaceId, player, friend.Id)
end)
end
end
-- Update display whenever the friend list is updated
displayFriends()
Periodically Updating the Friend List:
You might want to periodically check for updates to the friend list. You can do this using a loop with a wait, but be mindful of rate limits and performance.
luaCopy code
while wait(60) do -- Update every 60 seconds
updateFriendsList()
displayFriends()
end
Important Notes:
Teleportation Limitations: The Teleport method won’t directly take you to the same server as your friend due to Roblox’s privacy and security systems. This is a significant limitation.
API Restrictions: There are limitations in the Roblox API regarding what information you can access about where friends are playing, especially with respect to specific server instances.