Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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
0
You're such an hero. User#11440 120 — 8y
1
An hero, I gotcha. JamesLWalker 297 — 8y
1
Just take everything out of the "While true do" except the wait and a call to countBadges. GoldenPhysics 474 — 8y

2 answers

Log in to vote
1
Answered by
DevSean 270 Moderation Voter
8 years ago
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)
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? minetrackmania 186 — 8y
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 DevSean 270 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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)

Answer this question