Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

cool

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

2 comments

0

^

0

line 5 - argument #1  should be a player object for socialservice:promptgameinvite

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.