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)
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
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
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)