Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get the friend inviter to come out from a UI button click?

Asked by 4 years ago
Edited 4 years ago
while script.Parent.Visible == true do
    wait(1)
game.Players.LocalPlayer:WaitForChild("PlayerGui").Background.friends.MouseButton1Click:Connect(function(player)
local SocialService = game:GetService ("SocialService")
SocialService:PromptGameInvite(player)
SocialService.GameInvitePromptClosed:Connect (function(player,ids)
end)
    end)
end

The error is: attempt to index nil with 'WaitForChild' - line 3

3 answers

Log in to vote
1
Answered by 4 years ago

Use a local script and put it in the button. The script should be this:

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

function onButtonPressed()

    local Success, result = pcall(
        function()
            return SocialService:CanSendGameInviteAsync(player)
        end

    )

    if result == true then
        SocialService:PromptGameInvite(player)
    end
end

button.MouseButton1Click:Connect(onButtonPressed)
button.TouchTap:Connect(onButtonPressed)
0
cool Jakob_Cashy 79 — 4y
Ad
Log in to vote
0
Answered by
LaysCo 61
4 years ago

Make sure you are using a localscript and not a script as you can not get PlayerGui in a script.

0
^ WideSteal321 773 — 4y
0
line 5 - argument #1  should be a player object for socialservice:promptgameinvite Jakob_Cashy 79 — 4y
Log in to vote
0
Answered by 4 years ago

You can do

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

function OnPressed()
local success, err = pcall(function()
        function()
            Service:CanSendGameInviteAsync(player)
        end)
    end)
end)

if success == true then
    Service:PromptGameInvite(player)
else
    print(err)
end

Button.MouseButton1Click:Connect(OnPressed)
Button.TouchTap:Connect(OnPresed)

The code above is the code that will work, you can also just look at the thing below to help you out that's the comments.

local player is getting the player, and button is your button that you should use in this context. Service is the Social Service!

you can then function that into a pcall, and return the Server:CanSendGameInviteAsync(player), player can be localized above and can just do local players = game.Players.LocalPlayers

After that you can Prompt the game invite by simply doing Service:PromptGameInvite(player) which prompts it to that one player bringing the player invite thing up. Then whenever the button is pressed (the UI Button, you can just do Button.MouseButton1Click:Connect(OnPressed) and (Button.TouchTap:Connect(OnPressed)

Remember, that you can only do that if you function the OnPressed, aka

function OnPressed() -- Code Here / Pcall, and CanSendGameInviteAsync() end)

if the result == true then you can prompt the game invite, result is equal to anything you call in the pcall, (aka, success, worked, passed, connected etc) and then you can add an else statement and print(err) or the error message you got. end)

Answer this question