Give player a badge when their friend joins

The title says it all.
I have already tried scripting something like this in the past with articles on the roblox wiki where there would be a local script in starterplayerscripts that would check if someone followed the player then i would fire a event then on a serverscript it checks that data sent from the local script (which would be the players user id and the user id of the person that followed them) to see if the two players are friends with each other then it would check if they had the badge or not and give it to them if not and print that they are friends if they were and print that they weren’t friends if they weren’t. So then after I tested the game with a friend it printed nothing and gave no badge, am I doing something wrong or what?

Just do something along these lines:


game.Players.PlayerAdded:Connect(function(player)
	for i, player2 in pairs(game.Players:GetPlayers()) do
              if player2:IsFriendsWith(player.UserId) then
                    BadgeService:AwardBadge(player.UserId, badgeId) --Put Your Badge Id here
                   BadgeService:AwardBadge(player2.UserId, badgeId) --Put Your Badge Id here
              end
        end
end)
1 Like

I’m sorry that this is so messy. I made it in the forum.

This should be a server script right?

Yes. Put it in ServerScriptService.

Yes, badges cannot be awarded via localscripts.

Alright I try this once I get on studio later night, thanks!

The script you made might not know badgeservice or the id so in case anyone uses the script use this instead to prevent confusion, also place this script in ServerScriptService

local BadgeService = game:GetService("BadgeService")
local badgeId = 0 --Replace the 0 with the id of your badge
    
game.Players.PlayerAdded:Connect(function(player)
     for i, player2 in pairs(game.Players:GetPlayers()) do
              if player2:IsFriendsWith(player.UserId) then --I recommend you make an if statement to check if the players already own the badge
                    BadgeService:AwardBadge(player.UserId, badgeId)
                   BadgeService:AwardBadge(player2.UserId, badgeId) 
              end
        end
end)