Badge awarder script for attending a live event

So I’m trying to make a cript where players get a badge when they attend an live event but to make sure it works (it’s a live event),I don’t want to test it since I need to change a lot of my scripts.
Also can someone please check the last three lines,there’s an error.
So here’s the script,can anyone help me check?

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

while true do Wait(0) 
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) 

function onEntered(player)
	wait(1)
	if not badgeService:UserHasBadge(player.UserId, badgeID) then
		badgeService:AwardBadge(player.UserId, badgeID)
	end
end

	if Days==0 and Hours==0 and Minutes==0 and Seconds==0 or Time < 0 then
		game.PlayerAdded:Connect(onEntered) -- PlayerAdded is not a valid member of DataModel "2022 New Year Countdown" 
	end 
end
	
print("It's working!")
1 Like

Just posting properly formatted code for readability, real reply coming :slight_smile:

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

while true do
    Wait(0)
    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)

    function onEntered(player)
        wait(1)
        if not badgeService:UserHasBadge(player.UserId, badgeID) then
            badgeService:AwardBadge(player.UserId, badgeID)
        end
    end

    if Days == 0 and Hours == 0 and Minutes == 0 and Seconds == 0 or Time < 0 then
        game.PlayerAdded:Connect(onEntered) -- PlayerAdded is not a valid member of DataModel "2022 New Year Countdown" 
    end
end

print("It's working!")

Should be game:GetService("Players").PlayerAdded:Connect(onEntered)

Edit: WRONG PERSON SORRY,

@Cypacic Error is that you forgot to use PlayerAdded on the player service

1 Like

Hmm, I think if its an error on the last 3 lines it might be something to do with the print() at the end.

I thought I made it obvious but it’s actually here,my mistake.

game.PlayerAdded:Connect(onEntered) -- PlayerAdded is not a valid member of DataModel "2022 New Year Countdown"

Okay, so your code for calculating the remaining time until the event begins is correct:

local Time = TargetTime - Tick()

But your code has no concept of how long the event will last. There’s no way to check if the event is over or not.

Your code for giving players badges when they join if the event is currently ongoing is incorrect. It’s likely to set up the connection several times, meaning it will try giving joining players the badge several times. Could still work, since awarding badges is idempotent (doing it many times is the same as doing it once). Still, it confuses what exactly is supposed to happen when. It also doesn’t award the badge to players that are already in the server when the event begins, and doesn’t stop giving joining players the badge when the event ends.

How do I fix it again?
Im kinda new at scripting

Error is now gone but it still doesn’t print anything/work

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