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

How to regen a coin (with collectionservice), after a cooldown while making other coins work?

Asked by 1 year ago
Edited by Xapelize 1 year ago

I've been trying to find out the problem but I couldn't quite think of a solution to this problem. This is my code:

local CollectionService = game:GetService("CollectionService")
local CoinsTag = CollectionService:GetTagged("Coins")
local debounce = true

for _, taggedCoin in pairs (CoinsTag) do 
    taggedCoin.CanCollide = false
    taggedCoin.Touched:Connect(function(otherPart)
        if otherPart.Parent.Humanoid then
            if debounce == true then
                debounce = false
                local playerName = otherPart.Parent.Name
                local player = game.Players:FindFirstChild(playerName)
                player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 20
                taggedCoin.Transparency = 1
                wait(15) -- Cooldown time
                taggedCoin.Transparency = 0
                debounce = true
            end
        end
    end)
end

My question is, how do I make other coins work when I gave a cooldown time to one coin using collection service?

0
task.Spawn() i think Puppynniko 1059 — 1y

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
1 year ago

Create a debounce for each of the coins!

You can do that by moving the debounce variable inside the pairs loop, so in every loop, it will create a new debounce variable (for the coin!)

local CollectionService = game:GetService("CollectionService")
local CoinsTag = CollectionService:GetTagged("Coins")

for _, taggedCoin in pairs (CoinsTag) do
    local debounce = true

    taggedCoin.CanCollide = false
    taggedCoin.Touched:Connect(function(otherPart)
        if otherPart.Parent.Humanoid then
            if debounce == true then
                debounce = false
                local playerName = otherPart.Parent.Name
                local player = game.Players:FindFirstChild(playerName)
                player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 20
                taggedCoin.Transparency = 1
                wait(15) -- Cooldown time
                taggedCoin.Transparency = 0
                debounce = true
            end
        end
    end)
end
0
Thanks, this helped a lot! Bloxyses 57 — 1y
Ad

Answer this question