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
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)
Make sure you are using a localscript and not a script as you can not get PlayerGui in a script.
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)