Automatically Invite Players

local SocialService = game:GetService('SocialService')

function onPlayerAdded(plr)
	SocialService:PromptGameInvite(game.Players.LocalPlayer)
end

game.Players.ChildAdded:Connect(onPlayerAdded)

This script is supposed to automatically prompt player invite screen when the player joins. How do I make this work?

This cannot be on a Server Script if you’re using that, it needs to be a LocalScript

Also there’s really no need to detect for the ChildAdded event when detecting if a Player is added, you could just simply do

local Player = game.Players.LocalPlayer
local SocialService = game:GetService("SocialService")

SocialService:PromptGameInvite(LocalPlayer)

Or use the reference the API gives you:
https://developer.roblox.com/en-us/api-reference/function/SocialService/PromptGameInvite

1 Like

Basically think of attempting to get the Local Player in the Server-Side as this:

  • You’re trying to find a Screwdriver Item in a Grocery Store, it won’t be able to find it cause they’re both different
1 Like

one more thing: I did it and it gave me with jumble of code:
Unhandled promise rejection:

CorePackages.AppTempCommon.LuaChat.Utils.getFriendsActiveGamesPlaceIdsFromUsersPresence:13: attempt to index nil with ‘99361251’

CorePackages.Packages._Index.lua-promise-07b6e77b-1bb842c3.lua-promise:82 function new
CorePackages.Packages._Index.lua-promise-07b6e77b-1bb842c3.lua-promise:222 function andThen
CoreGui.RobloxGui.Modules.Settings.Pages.ShareGame.Thunks.ApiFetchUsersFriends:28
CorePackages.Packages._Index.roblox_rodux-a0ab4747-d06ccc8a.rodux.thunkMiddleware:10
CorePackages.Packages._Index.roblox_rodux-a0ab4747-d06ccc8a.rodux.Store:62
CoreGui.RobloxGui.Modules.Settings.Pages.ShareGame.Thunks.FetchUserFriends:19
CorePackages.Packages._Index.roblox_rodux-a0ab4747-d06ccc8a.rodux.thunkMiddleware:10
CorePackages.Packages._Index.roblox_rodux-a0ab4747-d06ccc8a.rodux.Store:62
CorePackages.Packages._Index.roblox_roact-rodux.roact-rodux.connect:149
CoreGui.RobloxGui.Modules.Settings.Pages.ShareGame.Components.ShareGameContainer:55
CoreGui.RobloxGui.Modules.Settings.Pages.ShareGame.Components.ShareGameContainer:30 function init
CorePackages.Packages._Index.roblox_roact.roact.Component:323 function __mount
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:363 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:93 function updateChildren
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:119 function updateVirtualNodeWithRenderResult
CorePackages.Packages._Index.roblox_roact.roact.Component:334 function __mount
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:363 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:93 function updateChildren
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:119 function updateVirtualNodeWithRenderResult
CorePackages.Packages._Index.roblox_roact.roact.Component:334 function __mount
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:363 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:93 function updateChildren
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:119 function updateVirtualNodeWithRenderResult
CorePackages.Packages._Index.roblox_roact.roact.Component:334 function __mount
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:363 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:93 function updateChildren
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:111 function updateVirtualNodeWithChildren
CorePackages.Packages._Index.roblox_roact.roact.RobloxRenderer:215 function mountHostNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:359 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:93 function updateChildren
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:119 function updateVirtualNodeWithRenderResult
CorePackages.Packages._Index.roblox_roact.roact.Component:334 function __mount
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:363 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:93 function updateChildren
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:119 function updateVirtualNodeWithRenderResult
CorePackages.Packages._Index.roblox_roact.roact.Component:334 function __mount
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:363 function mountVirtualNode
CorePackages.Packages._Index.roblox_roact.roact.createReconciler:399 function mountVirtualTree
CoreGui.RobloxGui.Modules.Settings.Pages.ShareGame.InviteToGamePrompt:71 function show
CoreGui.RobloxGui.CoreScripts/InviteToGamePrompt:42

Oh gosh use this then

local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
 
local function canSendGameInvite(targetPlayer)
	local res, canSend = pcall(SocialService.CanSendGameInvite, SocialService, targetPlayer)
	return res and canSend
end
 
local function promptGameInvite(targetPlayer)
	local res, canInvite = pcall(SocialService.PromptGameInvite, SocialService, targetPlayer)
	return res and canInvite
end
 
local function openGameInvitePrompt(targetPlayer)
	local canInvite = canSendGameInvite(targetPlayer)
	if canInvite then
		local promptOpened = promptGameInvite(targetPlayer)
		return promptOpened
	end
	return false
end
 
local function invitePromptClosed(senderPlayer, recipientIds)
	-- Handle custom logic for players invited by sender
end
 
openGameInvitePrompt(player)
SocialService.GameInvitePromptClosed:Connect(invitePromptClosed)
1 Like

I dont want button, i just want it to automatically prompt.

It should also depend on where you put the script, it should belong inside StarterPlayerScripts

1 Like

I did localscript inside of starterGUI

I think the error might be because of one of my friends, not the gui. idk though

Are you still getting any errors? If not, could you add some print() statements?

1 Like