Hello Developers, I’m trying to award badges to players in a table by using UserIds but I can’t seem to get it to work. I’ve look over some documents but can’t seem to help so some clarification here are appreciated. Please note that this doesn’t work so I’m trying to get some help! ^^
local script:
local eventWinnerID = {
1191626539,
4179414414
}
local LFunction = game:GetService("ReplicatedStorage"):WaitForChild("RFWA22Award")
game.Players.PlayerAdded:Connect(function(player)
local LocalPlr = game:GetService("Players").LocalPlayer
if table.find(eventWinnerID, LocalPlr.UserId) then
LFunction:FireServer(LocalPlr)
end
end)
Server:
local LFunction = game:GetService("ReplicatedStorage"):WaitForChild("RFWA22Award")
local badgeid = 2130230290
LFunction.OnServerEvent:Connect(function(LocalPlr)
game:GetService("BadgeService"):AwardBadge(LocalPlr.UserId, badgeid)
end)
I used a remote event to send message from client btw.
To award badges to players in Roblox whose userId is in a table, you can try something like this:
local players = game.Players:GetPlayers()
local userIds = {1191626539, 4179414414}
for i, player in pairs(players) do
if table.find(userIds, player.UserId) then
local badgeId = 2130230290
player:LoadBadge(badgeId)
player:AwardBadge(badgeId)
end
end
Correct it’s been a moment since I’ve used badge service tho I’m not certain on how to award badges, those last 2 lines need a fix probably, but yes this is the server script indeed.
That won’t work, since it only runs once when the server starts (probably when it has 0 players) oops, didn’t fully read your post
Here’s what I would do
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeId = 2130230290
local eventWinnerIDs = {
1191626539,
4179414414
}
function playerAdded(plr)
if table.find(eventWinnerIDs, plr.UserId) then
BadgeService:AwardBadge(plr.UserId, badgeId)
end
end
Players.PlayerAdded:Connect(playerAdded)
for _,plr in Players:GetPlayers() do
-- loop through all players that somehow joined before this script could finish running
playerAdded(plr)
end
Maybe you should add a check to see if the player already has the badge before giving it to them again, using :UserHasBadgeAsync().
You can do that like this, note the addition of the pcalls so if something goes wrong your script won’t throw an error and you can handle it appropriately.
You can do this like this in a Server script:
Firstly we will compare a player’s UserId when they join
local Players = game:GetService("Players") -- creating the players service if it doesnt exist yet
local UserIds = {1,2,3} -- change this
Players.PlayerAdded:Connect(function(Player) -- fires when a player joins
for _,v in pairs(UserIds) do
if v == Player.UserId then
end
end
end)
Afterwards we will need to add our proper handling of awarding badges, like this:
local BadgeService = game:GetService("BadgeService") -- creating the badge service if it doesnt exist yet
local Players = game:GetService("Players") -- creating the players service if it doesnt exist yet
local UserIds = {1,2,3} -- change this
local BadgeId = 0 -- change this to your badge id
Players.PlayerAdded:Connect(function(Player) -- fires when a player joins
for _,v in pairs(UserIds) do
if v == Player.UserId then
local success, errorMessage = pcall(function() -- properly wrapped in a pcall
BadgeService:AwardBadge(Player.UserId, BadgeId) -- award badges
print("Gave badge to user id: " .. Player.UserId)
end)
if not success then -- something went wrong
print("Failed to give badge to user id: ".. Player.UserId .. " with error message: " .. tostring(errorMessage))
end
end
end
end)
I highly recommend you read up on said pcalls to handle errors and avoid your code exiting, hope this helped!
Hi! Thank you for your detailed response, would something like this work for a check?
Players.PlayerAdded:Connect(function(Player) -- fires when a player joins
for _,v in pairs(eventWinnerIDs) do
if BadgeService:UserHasBadgeAsync(Player.UserId, badgeId) then
print("Player already has award badge")
elseif v == Player.UserId then
local success, errorMessage = pcall(function() -- properly wrapped in a pcall
BadgeService:AwardBadge(Player.UserId, BadgeId) -- award badges
print("Gave badge to user id: " .. Player.UserId)
end)
if not success then -- something went wrong
print("Failed to give badge to user id: ".. Player.UserId .. " with error message: " .. tostring(errorMessage))
end
end
end
end)
Yes you’ve nearly got it!
Although you should add another pcall for BadgeService:UserHasBadgeAsync
However, for clarity we are going to put this inside a helper function, like this:
local function playerHasBadge(Player)
local success, hasBadge = pcall(function() -- properly wrapped in a pcall
BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId) -- check for badges
end)
if not success then
print("Failed to check to see if user has the badge")
end
return hasBadge
end
and then in our code we just call the function:
for _,v in pairs(eventWinnerIDs) do
if playerHasBadge(Player) then continue end -- if they have the badge, skip over them
local success, errorMessage = pcall(function() -- properly wrapped in a pcall
BadgeService:AwardBadge(Player.UserId, BadgeId) -- award badges
print("Gave badge to user id: " .. Player.UserId)
end)
if not success then -- something went wrong
print("Failed to give badge to user id: ".. Player.UserId .. " with error message: " .. tostring(errorMessage))
end
end