I want to make a "You met the owner" badge [SOLVED]

There is no need to loop through more players, if you have already figured out that owner is present

Edit: Also your script does not take into account, for when players already plays the game, and the owner joins. I’m sure the currently playing players should get the badge aswell.

1 Like

I don’t think it’s relevant to break the loop after the conditional statement given that the for loop needs to be active each time a player joins.

Lets take this to PMs, so we don’t clog the thread.

1 Like

Have you considered this instead?
This minimizes the amount of loops needed to 1, instead of 1 each time a player joins.

local OwnerID = 100
local OwnerPresent = false
Players.PlayerAdded:Connect(function(plr)
	if plr.UserId == OwnerID then
		OwnerPresent = true
		for _, player in pairs(Players:GetPlayers()) do
			if player.UserId == OwnerId then continue end
			--award badge
		end
	elseif OwnerPresent then
		-- award badge
	end

end)
Players.PlayerRemoved:Connect(function(plr)
	if plr.UserId == OwnerID then
		OwnerPresent = false
	end
end)
2 Likes

Ok, thanks, but the script of @CommanderRanking should work too, no?

I put this script in ServerScriptService and it didn’t work.

Drop this in ServerScriptService in a non-local script

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badge = 0
local owner = 0 
local online = false

local function AwardBadge(player, badge)
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badge)
	end) if not success then print("hasBadge error") return
	end
		
	if not hasBadge then
		local success, badgeInfo = pcall(function()
			return BadgeService:GetBadgeInfoAsync(badge)
		end) if not success then print("badgeInfo error") return
		end
		
		if success then
			if badgeInfo.IsEnabled then
				local awarded, errorMessage = pcall(function()
					BadgeService:AwardBadge(player.UserId, badge)
				end)
				
				if not awarded then	warn("Awarding error:", errorMessage)
				else print("Awarding badge")
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	if player.UserId == owner then online = true 
		for _,p in pairs(game.Players:GetPlayers()) do
			AwardBadge(p, badge) task.wait(1)
		end
	elseif online == true then
		AwardBadge(player, badge)		
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	if player.UserId == owner then online = false end 
end)

Make sure to fill in the badge and player info and that you have a valid badge. This is tested and works well. If it don’t work for you it will also tell you why.

7 Likes

Ok thanks I’ll try that! Have a good day! :roblox_light:

oh, I forgot to set online off if the owner logs off … hold on
Hope that last part works didn’t test that. But, I updated the script.

Well you can edit your post :slight_smile:

Thanks again! :roblox_light:

It’s all there now … have fun!
The only thing I couldn’t test was if there was a lot of players on and the Owner joined. So I put a task.wait(1) … in that loop that may not be needed but, just in case. I was able to test with a few players on. And yes it gives it to you also. :wink:

Thanks again, that’ll help a lot! Have a nice day!!

1 Like

I added the script to my game but when I was in my game, then two of my friend joined me, not any got the badge. Do you know why? Sorry for asking for your help again, have a good day! :roblox_light:

I actually made this badge a long time ago, here is the code

local badge = game:GetService("BadgeService")
local badgeid = 0   --Badge id
local main = ""   --Owner name

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Name == main then
		for i,v in pairs(game.Players:GetChildren()) do
			if badge:UserHasBadgeAsync(v.UserId,badgeid) then
				else
				badge:AwardBadge(v.UserId,badgeid)
			end
		end
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local mainplayer = game.Players:FindFirstChild(main)
	if mainplayer and not badge:UserHasBadgeAsync(plr.UserId,badgeid) then
		badge:AwardBadge(plr.UserId,badgeid)
	end
end)
1 Like

Did you tried it? Because I’m bored to try all theses scripts and not any work… It should be in ServerScriptService?

Yes, It works completely fine for me. Paste this code in a server script in serverscriptservice

Ok thanks I’ll do that! But if the player change of name it won’t work?

That is the case when the owner changes the name, u can use the owner’s id instead of name.

so if I do the same but I replace

per

local main = 0000000000 --Replace this per owner ID

it’ll work?

Yes u just need to do a change with if statement

local badge = game:GetService("BadgeService")
local badgeid = 0   --Badge id
local main = 000   --Owner Id

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Id== main then
		for i,v in pairs(game.Players:GetChildren()) do
			if badge:UserHasBadgeAsync(v.UserId,badgeid) then
				else
				badge:AwardBadge(v.UserId,badgeid)
			end
		end
	end
end)
1 Like

Thanks I’ll try that! Have a good day!