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

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

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?

1 comment

0

task.Spawn() i think

1 answer

✓ Accepted answer

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

1 comment

0

Thanks, this helped a lot!

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