Hello I am learning to use this service trying to make it give me a medal if it finds my name, but the script does not work, does anyone know what is wrong?
The output shows nothing
local badgeService = game:GetService("BadgeService")
local badgeId = 2463675936
local players = game:GetService("Players")
for _, player in pairs(players:GetPlayers()) do
if player.Name == 'MatiasHarders' then
badgeService:AwardBadge(player.UserId, badgeId)
end
end
local badgeInfo = badgeService:GetBadgeInfoAsync(badgeId)
for _, player in pairs(players:GetPlayers()) do
if player.Name == 'MatiasHarders' then
print(badgeInfo.IsEnabled)
badgeService:AwardBadge(player.UserId, badgeId)
end
end
Edit:
I am pretty sure this will work because I got it straight off of the wiki.
local BadgeService = game:GetService("BadgeService")
local players = game:GetService("Players")
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awarded, errorMessage = pcall(function()
BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awarded then
warn("Error while awarding badge:", errorMessage)
end
end
else
warn("Error while fetching badge info!")
end
end
players.PlayerAdded:Connect(function(player)
if player.Name == 'MatiasHarders' then
awardBadge(player, badgeId)
end
end)
Another thing that might be happening is that the player has not loaded in yet when you are trying to find them. As @c_PRO said, it is best to use the PlayerAdded event.
Badges are virtual awards you can give to people in-game using the BadgeService, you, however are trying to use the service to award an item (trying to make yourself equip it???), which isn’t how it works: if you don’t understand what badges are, this page should explain it