Badge Awarding help

I recently added a badge to my Backrooms game that I want players to obtain during Blackouts. My current code (with irrelevant portions cut out to save space here) is this:

print("Script Started")
lightFolder = game.Workspace.Map.Lights
lights = {}
print("Script Functioning")
activeLight = 0
testmode = false
blackoutActive = game.Workspace.blackoutActive
blackoutActive.Value = false
blackoutPity = 0
blackoutCD = 0
id = 3867131789621169
badgeService = game.BadgeService
local plr = game.Players.LocalPlayer

--  Irrelevant functions here

function blackout()
	for i = 1, 255 do
		lightOff(i, false)
	end
	game.Workspace.Level0Music:Stop()
	game.Workspace.Blackout:Play()
	blackoutActive.Value = true
	blackoutCD += 1
	print("Blackout.")
	while blackoutActive.Value == true do
		if not badgeService:UserHasBadgeAsync(plr.UserId, id) then
			print("Badge awarded")
			badgeService:AwardBadge(plr.UserId, id)
		end
	end
end

-- Function triggers here

The script is in ServerScriptServer and when it reaches the badge awarding it returns this error:

4 Likes

You can’t access LocalPlayer in a server script, its only available from local scripts

If you want to give the badge to all players when the blackout happens then loop through Players:GetPlayers() and give the badge to each player.
You probably also don’t need to aware the badge within a while loop like you’ve got here, as it will try to give the badge many times, just award it once after setting blackoutActive to true

5 Likes

How exactly would I go about using this?

1 Like

Something like this, it should give the badge to every player in the server after the blackout has happened

1 Like

Still doesn’t work for some reason.

1 Like

Do you already own the blackout badge?

No, I’ve checked multiple times. I also had a friend playtesting with me who doesn’t have it yet either.

It is impossible to use Players.LocalPlayer in the server scripts, because there are no client which is related to this variable, therefore it’s just nil. You should find player with needed name in Players and provide it as an argument: Player = game.Players:FindFirstChild("YourNameHere")

I’m not awarding it to a specific player though, just all players in the server while the Blackout occurs.

What does the output say with @Piggywise’s solution?

The output doesn’t say anything other than the print statement’s output.

Is your badge id correct? I’ve never seen a badge id that long.

Neither have I, but it’s correct as far as I’m aware. I got it from the “Copy Asset ID” button.

-- https://create.roblox.com/docs/reference/engine/classes/BadgeService#AwardBadge
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 awardSuccess, result = pcall(function()
				return BadgeService:AwardBadge(player.UserId, badgeId)
			end)

			if not awardSuccess 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

local function blackout()
	for i = 1, 255 do
		lightOff(i, false)
	end
	game.Workspace.Level0Music:Stop()
	game.Workspace.Blackout:Play()
	blackoutActive.Value = true
	blackoutCD += 1
	print("Blackout.")

    for _, player in ipairs(game.Players:GetPlayers()) do
        awardBadge(player, id)
    end
end

Try this and see what the console spits out.

It returned “Failed to award badge”

Are you testing in a live game and not in studio/team test?

I tried both. In studio it returns that error and in the game it simply does nothing (I don’t know how to check output ingame so I’m assuming it has the same output.)

Make sure you are saving and publishing the new experience. As an owner, you can open the developer console (F9) and on the Log section, hit Server. Check if the error still occurs. If there are no errors, check again if you already have the badge.

I do save and publish quite frequently and I have checked and I don’t have the badge.

I added a debounce to the while loop and made a seperate function that gets looped as soon as the blackout value turns true.

Does this work?

print("Script Started")
lightFolder = game.Workspace.Map.Lights
lights = {}
print("Script Functioning")
activeLight = 0
testmode = false
blackoutActive = game.Workspace.blackoutActive
blackoutActive.Value = false
blackoutPity = 0
blackoutCD = 0
id = 3867131789621169
badgeService = game:GetService("BadgeService")

--  Irrelevant functions here

local function awardBadge(obj)
    if not badgeService:UserHasBadgeAsync(obj.UserId, id) then
        print("Badge awarded")
        badgeService:AwardBadge(obj.UserId, id)
    end
end

function blackout()
	for i = 1, 255 do
		lightOff(i, false)
	end
	game.Workspace.Level0Music:Stop()
	game.Workspace.Blackout:Play()
	blackoutActive.Value = true
	blackoutCD += 1
	print("Blackout.")
	while blackoutActive.Value == true do
		for _, plr in pairs(game.Players:GetChildren()) do
            awardBadge(plr)
        end
        task.wait(1)
	end
end

-- Function triggers here