How can I check if a player is in the server by userID

Hello! I’m making this badge to award to players when the creator is in the same server as them. But I don’t know how to do the scripting. Any suggestions on how to find a certain player in the server by their user ID? Thanks!

Use :GetPlayerByUserId()

3 Likes

Use a script like this

local creatorId = nil -- Replace nil with the userid  e.x( 19374723 )
local BadgeId = nil -- Replace this with the Id of the Badge

local BadgeService = game:GetService("BadgeService")

while true do
	task.wait()
	for i, v in pairs(game.Players:GetChildren()) do
		if v.UserId == creatorId then
			creator = v
			for ii, vv in pairs(game.Players:GetChildren()) do
				BadgeService:AwardBadge(vv.UserId, BadgeId)
			end
		end
	end
end

Tell me if you get any errors, this was made off the top of my head.

1 Like

OK I’ll try both of them tomorrow.

1 Like

@StarJ3M’s idea will work But this is better and efficient

local YourBadge = 0000000 -- your badge id
local Players = game:GetService("Players")
local PlayerUserId = PlayersGetPlayerByUserId(nil) -- yourUserId
 
if PlayerUserId then
      game:GetService("BadgeService"):AwardBadge(PlayerUserId, YourBadge)
end

Just came back to this to realize that @StarJ3M’s idea will work, but will be really bad for the server. @lilmazen1234’s idea won’t work at all as it only runs at the start, and will only award to the creator either way. Here is a version that would work and is the most efficient possible:

local badgeid = 00000 --badge id
local badges = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(plr)
    if plr.UserId == game.CreatorId then
        for i,v in pairs(game.Players:GetPlayers()) do
            if not badges:UserHasBadge(v.UserId, badgeid) then
                badges:AwardBadge(v.UserId, badgeid)
            end
        end
    else
        if game.Players:GetPlayerByUserId(game.CreatorId) then
            badges:AwardBadge(plr.UserId, badgeid)
        end
    end
end)
1 Like

Can you elaborate why yours is more usable? I want some recommendations for my future work.

Yours constantly loops through the players of the game and checks whether one of them has the userid of the creator, but you also do another loop to give the badges. Mine is more useable since it only goes when a player is added and if that player’s userid is the owner’s, it will give the badge to everyone in the server.

2 Likes

You can get the name for the ID with GetNameFromUserIdAsync, after verify if exist with FindFirstChild. (Sorry for my english)

local players = game:GetService("Players")

local function ifplayer(ID)
	local PlayerName = players:GetNameFromUserIdAsync(ID)
	if(players:FindFirstChild(PlayerName))then
		print("Exist");
	else
		print("No exist");
	end
end

Just bind the function

here is a more efficient method

local badgeService = game:GetService("BadgeService")
local playersService = game:GetService("Players")

local creatorId = 123456789
local creatorOnline = false
local badgeId = 123456789

playersService.PlayerAdded:Connect(function(player)
	if player.UserId == creatorId then
		creatorOnline = true
		-- when the creator enters the game loop all players already on this server and give them a badge
		for i, otherPlayer in playersService:GetPlayers() do
			if player == otherPlayer then continue end
			badgeService:AwardBadge(otherPlayer.UserId, badgeId)
		end
	elseif creatorOnline == true then
		badgeService:AwardBadge(player.UserId, badgeId)
	end
end)

playersService.PlayerRemoving:Connect(function(player)
	if player.UserId ~= creatorId then return end
	creatorOnline = false
end)

in this method we keep a variable stored when the creator logs in we set to true and when they log out we set to false so that when a new player logs in we don’t need to loop all players to find the creator we just check the variable and if its set to true we know the creator is online also when the creator logs in that is the only time we need to loop the players to make sure to give badges to all players that are already online

there is no need to use UserHasBadgeAsync because

1 Like

More like:

local creators = {"kdopdaux1803","nzrw1","steakwad","LoukaTigre_ps3","PiexelatedMind"} -- You can put anyone in there (kdopdaux1803 (Me (aka Cyril) could be it(nzrw1 (aka Nzrw1) could be it(steakwad (aka steak) could be it(LoukaTigre_ps3 (aka Louka) could be it(PiexelatedMind (aka steakIsInYourWalls) could be it too) So like you can post everyone in there, i only putted 5 people but that is ok.

This line of this script is composed of 397 letters so this is why the line of it is so long…

1 Like

I was giving him an example What he could do, Not the whole script.

Thanks, @Kaid3n22 and @StarJ3M . It worked perfectly (I made another account to test it).

1 Like

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