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:
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
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")
-- 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
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 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