Hello, I have a met owner badge script that doesnt work, and I don’t know why, here is the script
local BadgeService = game:GetService("BadgeService")
game.Players.PlayerAdded:Connect(function(Localplr)
if game.Players:FindFirstChild("zayzaycoco") then
for i,v in pairs(game.Players:GetPlayers()) do
BadgeService:AwardBadge(v.UserId, 2124725311)
end
end
end)
You could just check if the Owner’s name is equal to the player that joined
--Should belong in ServerScriptService
local BadgeService = game:GetService("BadgeService")
local OwnerName = "zayzaycoco"
game.Players.PlayerAdded:Connect(function(Player)
if Player.Name == OwnerName then
for _, PlayerToReward in pairs(game.Players:GetPlayers()) do
BadgeService:AwardBadge(PlayerToReward.UserId, 2124725311)
end
end
end)
What we’re doing is just basically implementing a simple conditional check with comparing if 2 String Values are the same, if they are then we can reward the badge to all players
@ThatCodedDev That’s just a loop to get all the players in order to reward them all the badge
Maybe try this. `local badgeService = game:GetService(“BadgeService”)
local id = 12345
local ownerId = 12345
local isInGame = false
game.Players.PlayerAdded:Connect(function(plr)
if plr.UserId == ownerId then
isInGame = true
for i, plrInGame in pairs(game.Players:GetPlayers()) do
badgeService:AwardBadge(plrInGame.UserId, id)
end
elseif isInGame then
badgeService:AwardBadge(plr.UserId, id)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
if plr.UserId == ownerId then
isInGame = false
end
One problem with the answer that is marked as solution is that players that join the game after the owner has joined will not get the badge. It only works for people that were in the game before the owner.
--Should belong in ServerScriptService
local BadgeService = game:GetService("BadgeService")
local OwnerName = "zayzaycoco"
local OwnerJoined = false
game.Players.PlayerAdded:Connect(function(Player)
if Player.Name == OwnerName or OwnerJoined == true then
for _, PlayerToReward in pairs(game.Players:GetPlayers()) do
if not BadgeService:UserHasBadgeAsync(PlayerToReward.UserId, 2124725311) then
BadgeService:AwardBadge(PlayerToReward.UserId, 2124725311)
end
end
OwnerJoined = true
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
if Player.Name == OwnerName then
OwnerJoined = false
end
end)
This is good, I like it. I would probably pull out the ‘for loop’ and only run it once when the owner joins vs. looping through all the players every time any player joins.
Then for the PlayerAdded just have the owner check and/or the single player badge check.
IDK how much bandwidth is saved not checking every player for a badge every time someone joins, but it seems more efficient to not do that.
That line doesn’t keep it from cycling through the list of players every time anyone joins. It only keep it from cycling through if there is no owner in the server.
So basically what you’re saying is you’d loop through all players when the owner joins. But if the owner variable has already been set then you’d just give the joining player the badge, correct?
I’d probably rearrange it to work like this(not tested):
--Should belong in ServerScriptService
local BadgeService = game:GetService("BadgeService")
local OwnerName = "zayzaycoco"
local OwnerJoined = false
game.Players.PlayerAdded:Connect(function(Player)
if OwnerJoined == true then --owner already here, add the one player
if not BadgeService:UserHasBadgeAsync(Player.UserId, 2124725311) then
BadgeService:AwardBadge(Player.UserId, 2124725311)
end
end
if Player.Name == OwnerName then --owner just joined, award everyone in server:
for _, PlayerToReward in pairs(game.Players:GetPlayers()) do
if not BadgeService:UserHasBadgeAsync(PlayerToReward.UserId, 2124725311) then
BadgeService:AwardBadge(PlayerToReward.UserId, 2124725311)
end
end
OwnerJoined = true
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
if Player.Name == OwnerName then
OwnerJoined = false
end
end)
Yes, to save a little network async bandwidth. Probably not significant, but to avoid checking everyone over and over once they’ve already been checked and awarded.