Invite Friends Button not working... (Topbar Plus)

Hi, I’m using ForeverHd’s module TopbarPlus to make an Invite Friends button, but when the button is pressed it doesn’t call SocialService when clicked on.

I’ve tried looking for solutions and tried to fix this by myself, but nothing I tried worked…

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

local playerGui = player.PlayerGui

local replicatedStorage = game:GetService("ReplicatedStorage")

local iconModule = replicatedStorage.Icon
local Icon = require(iconModule)
local IconController = require(iconModule.IconController)

local Themes = require(iconModule.Themes)

local gui = playerGui:WaitForChild("TopbarGUI")

local InvFriendsIcon = Icon.new()
	:setImage(1030548665)
	:setLabel("Invite Friends!")
	:bindToggleItem(playerGui) 
	:setCaption("Invite your friends!")
	:setOrder(2)

local icons = IconController.getIcons()

for _, icon in pairs(icons) do
	IconController.clearIconOnSpawn(icon)
end

local button = InvFriendsIcon
button.toggled:Connect(function(...)
   SocialService:PromptGameInvite(player)
end)

I did that but I got this:

image

When your stuck its usually a good idea to read the API which can be found on this page. If you read it, it states that you should use Topbar Plus’s icon.selected event. I have fixed it below.

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

local playerGui = player.PlayerGui

local replicatedStorage = game:GetService("ReplicatedStorage")

local iconModule = replicatedStorage.Icon
local Icon = require(iconModule)
local IconController = require(iconModule.IconController)

local Themes = require(iconModule.Themes)

local gui = playerGui:WaitForChild("TopbarGUI")

local InvFriendsIcon = Icon.new()
	:setImage(1030548665)
	:setLabel("Invite Friends!")
	:bindToggleItem(playerGui) 
	:setCaption("Invite your friends!")
	:setOrder(2)
    :bindEvent("selected", function(self)
         print(("%s was selected!"):format(self.name))
     end)


local icons = IconController.getIcons()

for _, icon in pairs(icons) do
	IconController.clearIconOnSpawn(icon)
end

My apologizes, I took a quick look at it then realised that was not the case.

Icon.new does not return a Roblox instance, and therefore MouseButton1Click is not a valid event in this scenario. Refer to the official Topbar+ documentation for a list of valid “Icon” events:

You might be looking for the .selected event here, although I am not familiar with Topbar+'s API.

In relation to the quoted code above, there’s also no need to redefine InvFriendsIcon as button

2 Likes

Yeah, that didn’t work… :confused:

(3O chars)

I edited my original script, try that. Send a screenshot if there are any errors.

Nothing prints when I select it…

Replace your code with:

local button = InvFriendsIcon
button.toggled:Connect(function(...)
   SocialService:PromptGameInvite(player)
end)

Edit: I saw the TopBarPlus API, and I made it work.

It didn’t work…

(3O chars)

Do you get any errors in the output?

No, I don’t…

(3O charsss)

Here is a script that I even tested in my own game and it works perfectly. Make sure it is a local script inside of StarterPlayerScripts:


Code

local Icon = require(game:GetService("ReplicatedStorage").Icon)
local SocialService = game:GetService("SocialService")
local Player = game.Players.LocalPlayer
local icon = Icon.new()
:setLabel("Invite Friends", "deselected")
:setLabel("Open", "selected")
:setCaption("Invite your friends to join!")

local function onButtonPressed()
	local success, result = pcall(
		function()
			return SocialService:CanSendGameInviteAsync(Player)
		end
	)
	
	if result == true then
		SocialService:PromptGameInvite(Player)
	end
end

icon.selected:Connect(function()
	onButtonPressed()
end)

Let me know if it works!

1 Like

That did work! Thank you.

(3O chars)

1 Like