Identify if a player is not your friend

This is helpful, if you only want your game not to be published publictly (Only your friends can play) so lets get started.

firstly we will need a PlayerAdded function, insert a script in ServerScriptService then copy this function:

game.Players.PlayerAdded:Connect(function(player) --The parameter (player) is the player that will join your game.
        
end)

then we will need to define a variable on top the PlayerAdded function, name it like ‘MyUserId’, ‘UserId’ Etc.

local MyUserId = 2554983508 -- Replace your actual UserId

it would look something like this:


local MyUserId = 2554983508 --  Replace your actual UserId

game.Players.PlayerAdded:Connect(function(player) --The parameter (player) is the player that will join your game.
        
end)

after you’ve than that, you have two option, First option is if you want to kick the player when its not your friend. and the second option is to let them in or do some pranks if its your friend.

we will need to add this if else statement inside the PlayerAdded function:

if player:IsFriendsWith(MyUserId) then
      -- Do something if the player is your friend.
else
    print("Im not your friend! im kicking you.")
    player:Kick("Reason Here")
end

if you want to only kick the player if its not your friend use this if statement:

if not player:IsFriendsWith(MyUserId) then
   player:Kick("Reason Here")
end

choose an if statement then put that below the PlayerAdded Function.

local MyUserId = 2554983508 --  Replace your actual UserId

game.Players.PlayerAdded:Connect(function(player) --The parameter (player) is the player that will join your game.
     if player:IsFriendsWith(MyUserId) then
    -- Do something if the player is your friend.
     else
        print("Im not your friend! im kicking you.")
        player:Kick("Reason Here")
     end
     
     -- ...
end)

and then your DONE, I hope this tutorial helps.

Why not just set it to friends only?

1 Like

Im actually teaching the freshmans about the IsFriendsWith function and what it can do. so im not only talking about the playeradded function with isFriendsWith.

I agree with him, it seems like a lot of work to make an entire script, when there is a feature built into Roblox that only allows friends to see and join your game?

Just so you know, friends are now connections if you don’t see the option. It’ll now be called “Connections Only”.

the OP explained that the tutorial in general makes you better understand how to use the function, not just kicking the player if it’s not your friend if i understood correctly. Obviously the script he made would be unnecessary since the friends only option is there