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?
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