So I have this one script that controls all the buy buttons in this model, but whenever you touch one of the buy buttons, it's debounce affects all buttons, rather than the one you touched. I don't really know how to fix, and I would love to figure out how to.
local debounce = false local cooldowninterval = 2 for _, button in pairs(script.Parent:GetChildren()) do if button:FindFirstChild("Head") then button:FindFirstChild("Head").Touched:Connect(function(hit) if debounce then return end debounce = true local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) if player and player.Character then local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then local leaderstats = player:FindFirstChild("leaderstats") local cash = leaderstats:FindFirstChild("Cash") if leaderstats and cash then local item = button:FindFirstChild("Item") local cost = button:FindFirstChild("Cost") if cash.Value >= cost.Value then cash.Value = cash.Value - cost.Value local itemclone = game:GetService("ServerStorage")[item.Value]:Clone() itemclone.Parent = player.Backpack end end end end wait(cooldowninterval) debounce = false end) end end
You can add a boollean value instance in each button during the loop, so each button will have its individual debounce.
local cooldowninterval = 2 for _, button in pairs(script.Parent:GetChildren()) do if button:FindFirstChild("Head") then local Debounce = Instance.new('BoolValue',button) --Puts a debounce in the button Debounce.Name = 'Debounce' button:FindFirstChild("Head").Touched:Connect(function(hit) if Debounce.Value == true then return end Debounce.Value = true local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) if player and player.Character then local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then local leaderstats = player:FindFirstChild("leaderstats") local cash = leaderstats:FindFirstChild("Cash") if leaderstats and cash then local item = button:FindFirstChild("Item") local cost = button:FindFirstChild("Cost") if cash.Value >= cost.Value then cash.Value = cash.Value - cost.Value local itemclone = game:GetService("ServerStorage")[item.Value]:Clone() itemclone.Parent = player.Backpack end end end end wait(cooldowninterval) Debounce.Value = false end) end end
~SkeletalReality