Badges – Special Game Awards page with broken code

On the https://developer.roblox.com/articles/Badges-Special-Game-Awards 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 or ModuleScript eventually required by a Script , instead of a LocalScript)

  • 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?

6 Likes

I encountered this error today on the DevHub. The code block still attempts to use Players.LocalPlayer in a server sided script.

Of course, if you run the code in a LocalScript a warning is shown:
image

It’s a shame this hasn’t been fixed yet and hopefully this is seen – the initial post was around 7 months ago and it’s still an issue!

2 Likes

Hey, thanks for the nudge – we’ll get this fixed soon.

2 Likes

Thanks for bringing these matters to our attention. The badges script example in the article has been updated. :slight_smile:

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.