How to use Referral Program?

Hey Developers!

So this will be really dumb, since there’s a lot of official Roblox resources, but i can’t just get it… Basically i want to award a badge to inviter if the invited person joins the experience. I just don’t understand the whole context here and it seems really confusing to me…

I rode this 4 times but was not able to figure out how to implement the feature in the game: Friend referral system | Documentation - Roblox Creator Hub.

There are also 2 official videos, but they just seem confusing as I’m still a beginner with not a lot of experience. https://www.youtube.com/watch?v=qfWKYgO63OI https://www.youtube.com/watch?v=rVFmc8gxu4s

Can someone please better explain me how to implement this feature in or which steps to take?

I will be really happy for any help!

Thanks in advance,
-jeziskrista

Just replying so this topic will be on the top of the Scripting Support page…

Hello, this should be working correctly:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local referrerEvent = ReplicatedStorage:FindFirstChild("ReferralReceivedEvent")
local BADGE_ID = 0

function onPlayerAdded(player)
	local joinData = player:GetJoinData()
	local referredByPlayerId = joinData.ReferredByPlayerId

	-- Check if the player was invited through a referral
	if not referredByPlayerId or referredByPlayerId == 0 then
		return
	end
	-- Fire the referral event to the client, passing the inviter's ID
	referrerEvent:FireClient(player, referredByPlayerId)

	-- Reward the inviter
	local referrerPlayer = Players:GetPlayerByUserId(referredByPlayerId)
	if referrerPlayer then
		BadgeService:AwardBadge(referredByPlayerId, BADGE_ID)
	end
end

-- Connect the function to the PlayerAdded event
Players.PlayerAdded:Connect(onPlayerAdded)

Don’t forget to create :
A badge and put it’s asset id in the BADGE_ID variable
A RemoteEvent and put it inside the referrerEvent variable
(This script has to be in ServerScriptService)

1 Like

Thank you so much! I’ll try it asap and get right back to you.

Hmm. Maybe I’m doing something wrong… Should i put this script in replicated storage too?

function onPlayerAdded(player)

    local referredByPlayerId = player:GetJoinData().ReferredByPlayerId

    local referrerEvent: RemoteEvent = ReplicatedStorage:FindFirstChild("ReferralReceivedEvent")
    referrerEvent:FireClient(player, referredByPlayerId)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Also, i just realized when i copied the game invite from the game it never said any user id. So i there’s also possibility i somehow set up the event wrongly… The invite: https://www.roblox.com/share?code=_3fyyyigry5hry1xb88p2xqxqzkzir0huxs0x2bvgn6fn5hqa5&type=ExperienceInvite

I did something wrong in the function i’m going to fix it

1 Like

Oh. thanks. take your time. don’t hurry… :wink:

Okay i fixed it, i edited the script up there.

1 Like

Let me try it really quickly. I’ll get back to you.

Still nothing… This is weird… Is there a possibility that you could do a quick call with me? via. Discord or other platform. I want to show you how i set up the whole event etc…

I sent you a dm with my username

1 Like

Solved via DMs in Discord. Thanks idealcrime!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.