How do i make SocialService script only run once per person?

I have a social service script that gives you in game currency when you invite someone. The problem, is that if you invite someone twice it gives you twice the currency. How could i make it so you only can invite each player once?

Here is the portion of the script that gives the eggs:

SocialService.GameInvitePromptClosed:Connect(function(player, ids)
	local names = {}
	for _, id in pairs(ids) do
		player.leaderstats.Eggs.Value = player.leaderstats.Eggs.Value + (10) --* player.leaderstats["Egg Multiplier"].Value)
		table.insert(names, game:GetService("Players"):GetNameFromUserIdAsync(id))
	end
end)
1 Like

Try storing a table of people you’ve already invited and compare against that.

1 Like

I tried this:

for i,v in pairs(names) do

if game:GetService("Players"):GetNameFromUserIdAsync(id) ~= v then

player.leaderstats.Eggs.Value = player.leaderstats.Eggs.Value + (10) --* player.leaderstats["Egg Multiplier"].Value)

end

end

table.insert(names, game:GetService("Players"):GetNameFromUserIdAsync(id))

In the script above, names is being reset every time SocialService.GameInvitePromptClosed is being fired. Try putting it outside of that function.

you mean putting

local names = {}

at top of code?

The reason your code isn’t working is because you’re creating a new variable every time the GameInvitePromptClosed Event is firing. To fix this, you need to have a variable outside the Event.
The code below will fix that issue.

local names = {}
game:GetService('SocialService').GameInvitePromptClosed:Connect(function(player, ids)
 for i, player in next, ids do
     table.insert(names,game:GetService("Players"):GetNameFromUserIdAsync(player))
 end
end)

Should you be wanting to have it be once permanently, you will have to save the data to a datastore and have it check.

1 Like