Yes, my second time, because last time the script didn’t work so I decided to make a new topic
Code that I have (ServerScriptService):
while wait(1) do
for i,v in pairs(game.Players:GetChildren()) do
if v.leaderstats.Stage.Value >= 26 then
game:GetService("BadgeService"):AwardBadge(v.UserId,2125418931)
end
end
end
It works but it doesn’t check if the player already has the badge
What I want is the script to check if the player already has a badge
while wait(1) do
for i,v in pairs(game.Players:GetChildren()) do
if v.leaderstats.Stage.Value >= 26 then
if not game:GetService("BadgeService"):UserHasBadgeAsync(v.UserId,2125418931) then
game:GetService("BadgeService"):AwardBadge(v.UserId,2125418931)
end
end
end
end
local BadgeService = game:GetService("BadgeService")
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 success, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not success then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
for i,v in pairs(game.Players:GetChildren()) do
if v.leaderstats.Stage.Value >= 26 then
if BadgeService:UserHasBadge(v.UserId, 2125418931) then
print("already has badge")
else
BadgeService:awardBadge(v.UserId, 2125418931)
end
end
end
You could probably hook up a :GetPropertyChangedSignal(“Value”) instead. I don’t see why we need to call this every second. (This is replacing the true loop)
local BadgeService = game:GetService("BadgeService")
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 success, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not success then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
game:GetService("Players").PlayerAdded:Connect(function(plr)
if plr.leaderstats.Stage.Value >= 26 then
if BadgeService:UserHasBadge(plr.UserId, 2125418931) then
print("already has badge")
else
BadgeService:awardBadge(plr.UserId, 2125418931)
end
end
end)
Actually scratch that ^
You could make it award the player a badge upon hitting a part when the stage changes?
local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")
players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local stage = leaderstats:WaitForChild("Stage")
stage.Changed:Connect(function(newValue)
if newValue == 26 then
badgeService:AwardBadge(player.UserId, 2125418931)
end
end)
end)