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!