Badge awarder script for attending a live event

It’s game.Players.PlayerAdded and not game.PlayerAdded.

Mhh I think that is a Server Script so he cannot use the LocalPlayer.

so basically if I want to go through a list of stuff I just use that?

Yep! in pairs is the most used way to loop through tables and dictionaries!

1 Like

It worked! TY! (Hope everything goes well when it’s live) lol

1 Like

Anytime! If you have anymore issues don’t be afraid to maek another post!

1 Like

Sorry, super late :sweat_smile:

Here's how I'd implement it
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

--Constants
--Times are UNIX timestamps
local EVENT_START_TIME = script:GetAttribute("EventStartTime")--1609459200 --Fri Jan 01 2021 00:00:00 GMT+0000
local EVENT_END_TIME = script:GetAttribute("EventEndTime")--1618299262 --Sat Jan 02 2021 00:00:00 GMT+0000
local EVENT_BADGE_ID = script:GetAttribute("EventBadgeId")--1234567890 --ID of badge to be awarded to anyone playing during the event
local DO_DEBUG_PRINT = script:GetAttribute("DoDebugPrint")--true

debugPrint = DO_DEBUG_PRINT and print or function() end --If debug printing is disabled, debugPrint does nothing.
debugWarn  = DO_DEBUG_PRINT and warn  or function() end --If debug printing is disabled, debugWarn does nothing.

--Awards event badge to player if they don't have it already
function awardEventBadge(player)
    if not badgeService:UserHasBadge(player.UserId, EVENT_BADGE_ID) then
        debugPrint( ("Awarding badge to %s (%d)"):format(player.Name, player.UserId) )
        badgeService:AwardBadge(player.UserId, EVENT_BADGE_ID)
    else
        
        debugWarn( ("Tried awarding event badge to %s (%d), but they already had it!"):format(player.Name, player.UserId) )
    end
end

--Wait until event begins
do --Wrapped in do end block to avoid polluting namespace with timeToEvent, since it's only maintained during this block
    local timeToEvent = EVENT_START_TIME - tick()
    if timeToEvent > 0 then
        wait(timeToEvent)
    end
end

--Setup awarding badges to joiners
local awardBadgeConnection = Players.PlayerAdded:Connect(function(player)
    awardEventBadge(player)
end)

--Award badge to any current players
for _, player in pairs(Players:GetPlayers()) do
    awardEventBadge(player)
end

--Wait until event ends
do --Wrapped in do end block to avoid polluting namespace with timeLeftOfEvent, since it's only maintained during this block
    local timeLeftOfEvent = EVENT_END_TIME - tick()
    if timeLeftOfEvent > 0 then
        wait(timeLeftOfEvent)
    end
end

--Stop awarding badges to joiners
awardBadgeConnection:Disconnect()
awardBadgeConnection = nil
1 Like