Time badge (10 Minutes in server etc..) isn't working properly

Hello Developers,

Lastly i was working on time badge (Or basically badge that will be awarded when you’re 10, 30 or 60 minutes in server.)

The problem is that only 10 minute variant badge is awarded. The other two (30 and 60) aren’t awarded.

I tried reviewing the script, but was not able to find anything that may cause it.

10 minutes script:

local Time = 600 -- Here you can adjust how long a player needs to be in the game to get the badge.
local PlayersJoinedTime = {} 

local function awardBadge(player)
	if not player:FindFirstChild("BadgeAwarded") then
		local BadgeAwarded = Instance.new("BoolValue")
		BadgeAwarded.Name = "BadgeAwarded"
		BadgeAwarded.Parent = player

		game:GetService("BadgeService"):AwardBadge(player.UserId, BadgeID)
	end
end

local function trackJoinedTime(player)
	PlayersJoinedTime[player] = tick()
end

local function checkJoinedTime()
	for player, joinedTime in pairs(PlayersJoinedTime) do
		if tick() - joinedTime >= Time then
			awardBadge(player)
			PlayersJoinedTime[player] = nil 
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	trackJoinedTime(player)
end)

while true do
	checkJoinedTime()
	wait(1)
end

30 minutes script:

local Time = 1800 -- Here you can adjust how long a player needs to be in the game to get the badge.
local PlayersJoinedTime = {} 

local function awardBadge(player)
	if not player:FindFirstChild("BadgeAwarded") then
		local BadgeAwarded = Instance.new("BoolValue")
		BadgeAwarded.Name = "BadgeAwarded"
		BadgeAwarded.Parent = player

		game:GetService("BadgeService"):AwardBadge(player.UserId, BadgeID)
	end
end

local function trackJoinedTime(player)
	PlayersJoinedTime[player] = tick()
end

local function checkJoinedTime()
	for player, joinedTime in pairs(PlayersJoinedTime) do
		if tick() - joinedTime >= Time then
			awardBadge(player)
			PlayersJoinedTime[player] = nil 
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	trackJoinedTime(player)
end)

while true do
	checkJoinedTime()
	wait(1)
end
type or paste code here

60 minutes script:

local BadgeID = 147193319471618 -- Change to Your Badge ID
local Time = 3600 -- Here you can adjust how long a player needs to be in the game to get the badge.
local PlayersJoinedTime = {} 

local function awardBadge(player)
	if not player:FindFirstChild("BadgeAwarded") then
		local BadgeAwarded = Instance.new("BoolValue")
		BadgeAwarded.Name = "BadgeAwarded"
		BadgeAwarded.Parent = player

		game:GetService("BadgeService"):AwardBadge(player.UserId, BadgeID)
	end
end

local function trackJoinedTime(player)
	PlayersJoinedTime[player] = tick()
end

local function checkJoinedTime()
	for player, joinedTime in pairs(PlayersJoinedTime) do
		if tick() - joinedTime >= Time then
			awardBadge(player)
			PlayersJoinedTime[player] = nil 
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	trackJoinedTime(player)
end)

while true do
	checkJoinedTime()
	wait(1)
end

Note: 10 minutes and 30 minutes don’t show the badge id when copied. I have no idea why, but the lane with badge id is there.

I would be really happy for any help!

Thanks in advance,
jeziskrista

Hi jerizkrista,

In each script you create a boolvalue named BadgeAwarded. Since each checks the same bool value, the 10 minute script will award the badge and set the boolvalue preventing the subsequent checkers from working.

You have various options to fix this:

  • Make Unique bool values for each of them.
  • Use BadgeService:UserHasBadgeAsync() this will come with extra logic if you’re using the boolvalue to show visuals in-game (or other various logic you may need it for).
  • Disregard the badgeawarded check if you don’t have any need for indicators or logic in-game if they have the badge.

Good luck coding!

On an unrelated side note, you have three scripts essentially doing the same thing. Try and see if you can refactor it for easier scalability and maintaining. If you want to fix a bug in one, you have to change all 3 scripts and that can be annoying down the line! Or if you add let’s say, 10 more time checks, you’ll have to manage 13 scripts each time you need to make a change.

3 Likes

Thanks! I will try this as soon as possible.