How to add an invite friends button to your game

This topic is going to cover how to add a friend invite button to your game. This tutorial is for beginners and veterans alike.

Part 1: The GUI

First you need to create a button to be displayed on the screen. You can customize it however you want but you need to insert a LocalScript into it and call it InviteScript

Part 2: Scripting

So we are going to be using a feature in Roblox called the SocialService. The SocialService allows you to add social features like friend invites into your game.

We first need to get variables such as the player, SocialService and the button, so at the top of your script type:

local SocialService = game:GetService("SocialService")
local Players = game.players
local player = Players.LocalPlayer
local button = script.Parent

This will load everything we need into variables

Next we need to create the function to run when the button is pressed. To do that we are gonna use the button.Activated event. This will tell us when the button is pressed. So add:

button.Activated:Connect(function()

Make sure to add an “end)” at the end of the function

Now we need to make this function actually do something so first we need to check if the Player can invite players to the game(eg. they are on Xbox). So we add to the function:

local canInvite = SocialService:CanSendGameInviteAsync(player)
if canInvite then
	SocialService:PromptGameInvite(player)
end

The CanSendGameInviteAsync runs a built in function that checks if the player is eligible to send an invite. Then the PromptGameInvite(player) will prompt the user with a GUI to choose who to invite

And now you have a basic friend invite system. But you can go further with this…

Lets say a player tries to click the button but they aren’t allowed to invite players well how will they know? We can display a notification saying they cant invite friends.

Part 4: CannotSendGameInvite Notification

We need to add an elseif statement to the if statement so add

elseif not canInvite then
		
	end

This runs when the player can’t send an invite. Now you need to have a frame called CantInvite in a ScreenGui in startergui. This frame will be our notification so customize it to your liking. Position it at 0.5,0,-0.5,0

Inside the elseif statement we are going to Tween the GUI to have a nice animation when it appears. So add:

player.PlayerGui.ScreenGui.CantInvite:TweenPosition(UDim2.new(0.5,0,0.3,0))

This will animate our notification to above the center of the screen. But how do you we remove it? Well you need to add a button in the frame which will act as a close button. Inside that button add a localscript and add:

local button = script.Parent

button.Activated:Connect(function()
	script.Parent.Parent:TweenPosition(UDim2.new(0.5,0,-0.5,0))
end)

All this does is Tween the GUI out of frame so they cant see it

Outro

And now you should have a working invite button! Thank you for using my tutorial!

24 Likes

Final Scripts

Goes in invite button:

local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local button = script.Parent

button.Activated:Connect(function()
	local canInvite = SocialService:CanSendGameInviteAsync(player)
	if canInvite then
		SocialService:PromptGameInvite(player)
	elseif not canInvite then
		player.PlayerGui.ScreenGui.CantInvite:TweenPosition(UDim2.new(0.5,0,0.3,0))
	end
end)

Goes in close notification button:

local button = script.Parent

button.Activated:Connect(function()
	script.Parent.Parent:TweenPosition(UDim2.new(0.5,0,-0.5,0))
end)

You can download the place here to try it for your self:
invitebutton.rbxl (37.6 KB)

16 Likes

I never knew how to actually do this, so thanks for the post!

5 Likes

I have been making a roleplay game and I always needed this feature. Thank you!

4 Likes

Is there any difference between using button.MouseButton1Click instead of button.Activated here?

4 Likes

The latter is universal and passes an InputObject so you can read more information about the input, the former passes nothing, and is implied to only work on left mouse click, therefore excluding mobile and console players.

6 Likes

There actually a model on the toolbox for this… I mean really!

At least you are kind enough to give the download file so hey good :blush:

3 Likes

@ThunderCobra48 that’s amazing. Thank you so much for this!
Is it actually possible to create a friend request button inside an experience?
like an actual in-game button, not as a GUI Button.
And if so, how?

that would be sooo cool …

2 Likes