On the Badges | Documentation - Roblox Creator Hub page discussing how to use a badge, a code block used to describe how use it is broken. The codeblock is using broken code as it seems to be either:
-
A LocalScript calling BadgeService:AwardBadge (Badges must be awarded on the server, i.e. from a
Script
orModuleScript
eventually required by aScript
, instead of aLocalScript
) -
A ServerScript using Players.LocalPlayer
The code block in question is this:
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeID = 0000000 -- Change this to your badge ID
local function awardBadge()
local player = Players.LocalPlayer
local hasBadge = false
-- Check if the player already has the badge
local success, message = pcall(function()
hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge: " .. tostring(message))
return
end
if hasBadge == false then
BadgeService:AwardBadge(player.UserId, badgeID)
end
end
specifically, this code:
if hasBadge == false then
BadgeService:AwardBadge(player.UserId, badgeID)
end
and this code:
local player = Players.LocalPlayer
Could this post be updated, as this code wouldn’t work if it was a Script or if it was a LocalScript?