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!")
Just posting properly formatted code for readability, real reply coming
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!")
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.
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
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)
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
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
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