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.
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)
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.
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.
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!
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)
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)