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
1while script.Parent.Visible == true do
2    wait(1)
3game.Players.LocalPlayer:WaitForChild("PlayerGui").Background.friends.MouseButton1Click:Connect(function(player)
4local SocialService = game:GetService ("SocialService")
5SocialService:PromptGameInvite(player)
6SocialService.GameInvitePromptClosed:Connect (function(player,ids)
7end)
8    end)
9end

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:

01local button = script.Parent
02local SocialService = game:GetService("SocialService")
03local player = game.Players.LocalPlayer
04 
05function onButtonPressed()
06 
07    local Success, result = pcall(
08        function()
09            return SocialService:CanSendGameInviteAsync(player)
10        end
11 
12    )
13 
14    if result == true then
15        SocialService:PromptGameInvite(player)
16    end
17end
18 
19button.MouseButton1Click:Connect(onButtonPressed)
20button.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

01local player= game.Players.LocalPlayer
02local Button = (Button Here)
03local Service = game:GetService("SocialService")
04 
05function OnPressed()
06local success, err = pcall(function()
07        function()
08            Service:CanSendGameInviteAsync(player)
09        end)
10    end)
11end)
12 
13if success == true then
14    Service:PromptGameInvite(player)
15else
16    print(err)
17end
18 
19Button.MouseButton1Click:Connect(OnPressed)
20Button.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