Heya, I’m trying to determine if a person joined a game via an invite sent via socialservice, so I can give them rewards for joining via a game invite. Are we able to do that?
Thanks!
Basically after prompting you will have a connection which will run when the GameInvitePrompt
gets closed.
We will use SocialService.GameInvitePromptClosed:Connect()
for this.
-- Client:
local function gameInvitePromotClosed(player, recipientIds)
-- send recipientIds to server
Event:FireServer(recipientIds)
end
-- Server:
local PlayerService = game:GetService("Players")
local invitedIds = {} -- invite[playerInstance] = recipientIds
local function findId(Id)
local playersWhoInvited = {}
for playerWhoInvited, Ids in pairs(invitedIds) do
if table.find(Ids, Id) then
table.insert(playersWhoInvited, playerWhoInvited)
end
end
return playersWhoInvited
end
local function mergeTables(t, t2)
for _, v in ipairs(t2) do
-- No repeated ids
if not table.find(t, v) then
table.insert(t, v)
end
end
return t
end
local function onEvent(player, recipientIds)
if invitedIds[player] then
invitedIds[player] = mergeTables(invitedIds[player], recipientIds)
else
invitedIds[player] = recipientIds
end
end
PlayerService.PlayerAdded:Connect(function(player)
-- Have a check if both players are friended if they are not ban the player who sent the invite for exploiting; I won't add this
local id = player.UserId
local playersWhoInvited = findId(id)
for _, playerWhoInvited in pairs(playersWhoInvited) do
-- Reward player, you can give players less reward if multiple invited them:
-- Reward * 1 / #playersWhoInvited
end
end)
PlayerService.PlayerRemoved:Connect(function(player)
-- invitedIds[player] = nil
end)
The scripts can be optimized in lots of ways this is just the basics.
1 Like
Doesn’t that return a nil table now? [ENABLED as of May 28] SocialService Game Invites are Live
1 Like