Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to makes this check over and over each 2 seconds?

Is their anyway make this checking every 2 seconds, because now it only counts when an player joins but I have over 150 badges so when an player find a new one i want the value goes up but now it doesn't check agian each 2 seconds. Someone know how? Btw I do like to not change line 2 beacuse I already did put in the orginal one 150 badges. Thanks alot!

local BadgeService = game:GetService("BadgeService")
local badges  = {229929415, 229929797, 229929912}
while true do
wait (2)
local function countBadges(player)
    local numBadges = 0
    for _, badge in next, badges do
        if BadgeService:UserHasBadge(player.userId, badge) then
            numBadges = numBadges + 1
        end
    end
    return numBadges
end

game.Players.PlayerAdded:connect(function(player)
    local stats = player:WaitForChild("leaderstats")
    local cash = stats:WaitForChild("Badge")
    cash.Value = countBadges(player)
end)
end

3 comments

0

You're such an hero.

1

An hero, I gotcha.

1

Just take everything out of the "While true do" except the wait and a call to countBadges.

2 answers

✓ Accepted answer
local BadgeService = game:GetService("BadgeService")
local badges = {229929415, 229929797, 229929912}

local function countBadges(player)
	local numBadges = 0
	for _, badge in next, badges do
		if BadgeService:UserHasBadge(player.UserId, badge) then
			numBadges = numBadges + 1
		end
	end
	return numBadges
end

game.Players.PlayerAdded:connect(function(player)
	local stats = player:WaitForChild("leaderstats")
	local badgeCount = stats:WaitForChild("Badge")
	badgeCount.Value = countBadges(player)
	while wait(2) do
		badgeCount.Value = countBadges(player)
	end
end)

2 comments

0

My only problem is it takes like one minute until it changes whatever time i get it how can I make it faster have any idea?

0

I think you might find the UserHasBadge function caches the result meaning it takes a minute to update, a solution would be to put a line of code in the badge givers to +1 to the badge count and use this code without the loop

This might work:

function countBadges(player)
    local numBadges = 0
    for _, badge in next, badges do
        if BadgeService:UserHasBadge(player.userId, badge) then
            numBadges = numBadges + 1
        end
    end
    return numBadges
end

local BadgeService = game:GetService("BadgeService")
local badges  = {229929415, 229929797, 229929912}

game.Players.PlayerAdded:connect(function(player)
    local stats = player:WaitForChild("leaderstats")
    local cash = stats:WaitForChild("Badge")
    cash.Value = countBadges(player)

    while true do
	wait (2)
	countBadges(player)
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.