So, it seems like I’m reaching a limit for DataStore requests because I don’t know how to optimize my code.
print("Script started running.")
local playerService = game:GetService("Players")
local badgeService = game:GetService("BadgeService")
local badgeId1 = 4303832512527984 -- Badge ID for 100 seconds
local badgeId2 = 952021745612171 -- Badge ID for 1000 seconds
local badgeId3 = 1104558979106111 -- Badge ID for 10000 seconds
local function AwardBadge(player, badgeId)
if not badgeService:UserHasBadge(player.UserId, badgeId) then
local success, result = pcall(function()
badgeService:AwardBadge(player.UserId, badgeId)
end)
if success then
print("Badge awarded to", player.Name, "with badge ID:", badgeId)
else
warn("Failed to award badge to", player.Name, "with badge ID:", badgeId, "Error:", result)
end
else
print(player.Name, "already owns badge with ID:", badgeId)
end
end
local function CheckAndUpdateBadge(player, bestTime)
if bestTime >= 100 then
AwardBadge(player, badgeId1)
end
if bestTime >= 1000 then
AwardBadge(player, badgeId2)
end
if bestTime >= 10000 then
AwardBadge(player, badgeId3)
end
end
local function OnBestTimeChanged(player, bestTime)
print("Best Time for", player.Name, "changed to", bestTime)
task.wait(1)
CheckAndUpdateBadge(player, bestTime)
end
local function OnCharacterAdded(character)
local player = playerService:GetPlayerFromCharacter(character)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local bestTimeStat = leaderstats:FindFirstChild("Best Time")
if bestTimeStat then
bestTimeStat.Changed:Connect(function()
task.wait(1)
OnBestTimeChanged(player, bestTimeStat.Value)
end)
end
end
end
end
playerService.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
task.wait(1)
OnCharacterAdded(character)
end)
end)
for _, player in ipairs(playerService:GetPlayers()) do
local character = player.Character
if character then
task.wait(1)
OnCharacterAdded(character)
end
end
Any help would be appreciated