I need help with Player:GetFriendsOnline()

I want to use Player:GetFriendsOnline() so that the player can teleport to a friend’s game if any of their friends are playing a game, but it does not work please help

Local script:

for i,v in pairs(game.Players.LocalPlayer:GetFriendsOnline(200)) do
    if v.GameId ~= nil then
        game:GetService("TeleportService"):TeleportToPlaceInstance(v.PlaceId, v.GameId, game.Players.LocalPlayer)
        break
    end
end
2 Likes

Try printing out what v, and v.GameId, gives you inside the loop?

Ok I did that and v prints a table that looks like this

{
    ["DisplayName"] = "unicornio_oli09",
    ["GameId"] = 525382208,
    ["IsOnline"] = true,
    ["LastLocation"] = "Dance Off",
    ["LastOnline"] = "2021-04-05T15:57:47.4434928Z",
    ["LocationType"] = 4,
    ["PlaceId"] = 1297992014,
    ["UserName"] = "unicornio_oli09",
    ["VisitorId"] = 2411184979
}

And v.GameId is just a bunch of numbers like 525382208 and I think thats whats supposed to be passed as the job id when i’m doing TeleportToPlaceInstance()

1 Like

Did you try this in studio or in a live game?

Are you getting any errors at all? You could try using the regular Teleport function if that changes anything

I tried this in a live game and doesnt work

I’m getting the error that says “Teleport failed because The place is restricted” but I know its not restricted because i can join that person perfectly fine when I do it from the roblox site

Here’s what my guess is:

  • If your friend is in a separate place apart from the starting place, you’re not able to join them that way cause they’re in a different place which results in that error

Could you try this?

for i,v in pairs(game.Players.LocalPlayer:GetFriendsOnline(200)) do
    if v.GameId ~= nil then
        game:GetService("TeleportService"):Teleport(v.GameId, game.Players.LocalPlayer)
        break
    end
end

I tried that and it took me to a completely different game ################### - Roblox this one apparently, which is the same game id as the one in the table I posted above

I also tried to do a teleport to a friend’s game that has only the starting place and it gives me the same error

Hm, try this? If that doesn’t work, try doing v.PlaceId for teleporting again

for i,v in pairs(game.Players.LocalPlayer:GetFriendsOnline(200)) do
    if v.GameId ~= nil and v.IsOnline == true then
        game:GetService("TeleportService"):Teleport(v.GameId, game.Players.LocalPlayer)
        break
    end
end

Ok it worked… but didnt teleport me to my friend’s game :slightly_frowning_face:

Then your friend is probably in a different separate place apart from where you first start on

There isn’t really much else I can think of, since attempting to teleport to a different place instance would result as an error

I guess you need to use PlaceId, not GameId.

local player = game:GetService("Players").LocalPlayer

for _, v in ipairs(player:GetFriendsOnline(200)) do
    if not v.IsOnline then continue end
    if not v.PlaceId then continue end

    game:GetService("TeleportService"):Teleport(v.PlaceId, player)
    break
end

Also make sure that the “Allow third party teleports” is enabled in the place to which you are trying to teleport the player to.

Try this out?

local tpServ = game:GetService("TeleportService")

for i,v in pairs(game.Players.LocalPlayer:GetFriendsOnline(200)) do
    if v.GameId ~= nil then
		local instance,_,placeId,jobId
		local success, result = pcall(function()
			 instance,_,placeId,jobId = tpServ:GetPlayerPlaceInstanceAsync(v.VisitorId)
		end)
		if success then
			tpServ:TeleportToPlaceInstance(placeid, jobid, game.Players.LocalPlayer)
			break
		end
    end
end

I did some research and saw about GetPlayerPlaceInstanceAsync, which returns 4 things, if the player is in an instance, an error if any, the placeid of where they are, and the jobid/instanceid, and its purpose is exactly what you’re wanting, to teleport to someone

1 Like

GetPlayerPlaceInstanceAsync only works for places that are in the same game as the current place. Thanks for the help though.

I’d assume that he would enable “Third Party Teleports”, since he was able to teleport to a place but not to his friend’s game

@mymommakesbathbombs4
Overall, I don’t believe it’s possible to join a friend if they are not in the starting place from where they first join the game from; otherwise you just join the starting place that the PlaceId gives you

Ok I think i found out why its not working…

So TeleportToPlaceInstance requires a JobId as its second parameter, which looks like this: "21b1653d-ffcb-48f5-a0fa-2e25cac8375a"

However the “GameId” that comes from GetFriendsOnline doesnt return the JobId, but instead the universe ID of the game, although on the DevHub page it says that Player | Roblox Creator Documentation returns the JobId as GameId

Interestingly enough, I came across another post that describes this same issue Player:GetFriendsOnline() not acting as stated on wiki