Badge awarder script for attending a live event

The print statement is under an infinite loop, the print is never going to be reached unless the loop is broken

so is the script ok or is it still broken since I can’t tell cuz it’s a loop (like u mentioned)

There’s an unneeded Wait(0) since you already have a wait(), and again, you should take a look at what @ThanksRoBama mentioned since he describes the issues you have right now, specifically how players will still earn the badge even if the event ended and players already i nthe game will not earn it. Did you mean to make it so when

if Days == 0 and Hours == 0 and Minutes == 0 and Seconds == 0 or Time < 0 then

Is met the players i ngame get the badge?

So basically Im making a new year celebration kinda thing and when it reaches 0 (new year) the players who ATTENDED the countdown gets the badge (right at 00:00)

Then can’t you do this?

local TargetTime = 1609459200 --- Target/Event Unix Timestamp, 1609459200 for example.
local mFloor = math.floor
local Tick = tick
local Stop = 0
local badgeID = 1234567890 -- My badge ID (ofc not this lol)
local badgeService = game:GetService("BadgeService")

while true do
    wait()
    local Time = TargetTime - Tick()
    local Days = mFloor((Time / 60 / 60 / 24) % (365 + 0.2425))
    local Hours = mFloor((Time / 60 / 60) % 24)
    local Minutes = mFloor((Time / 60) % 60)
    local Seconds = mFloor(Time % 60)

    if (Days == 0 and Hours == 0 and Minutes == 0 and Seconds == 0) or Time < 0 then
        for _,player in pairs(game:GetService("Players"):GetPlayers()) do
			if not badgeService:UserHasBadge(player.UserId,badgeID) then
				badgeService:AwardBadge(player.UserId,badgeID)
			end
		end
		break
    end
end

print("It's working!")

So it loops throuh the players i nthe game at the end of the event and if they don’t have the badge, award it

ok I’ll try that later but you reminded me about one thing and might get off topic
what is this ‘for_,[object] in pair’ script/command,I never knew when and how to use

It’s a loop that works specifically for tables and dictionaries, it gives 2 things, the index and the value in that index the loop is currently out. Since we’re using it for a table, it basically works like this

local fruits = {"Apple", "Banana", "Mango"}

for i,v in pairs(fruits) do
    print(i,v)
end

--Output Results
1 Apple
2 Banana
3 Mango

GetPlayers() returns all the players ingame as a table

btw is this script…
< 23:59 = Can’t get Badge
00:00 = GET BADGE
AFTER 00:01= Can’t get Badge

That’s what I’m doing, when the time is up, it goes through the players i ngame, give them the badge if they don’t have it and then once it’s done looping through all of them, break the loop so it doesn’t continue it anymore

lol why do you keep saying i nagme

ok but can u tell me when to use the ‘for_,in pair’ command? tysm

I keep typing fast and I keep making that mistake somehow haha

If you want to loop through everything in a table or dictionary, that’s when you want to use in pairs, also _ and v can be renamed to anything you want if needed, _ is typically used to show the reader that thing is never used

@NinjaFurfante07 That has already been mentioned already

You need to change “game.LocalPlayer” to “game.Players.LocalPlayer” or game:GetService(“Players”).LocalPlayer

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