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

Debounce affects all buttons rather than the one touched?

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

2 comments

0

It's a part...

0

^ then I have a question...game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) <-- that means that all the players from the game would need to touch the brick at the same time????? ._.

1 answer

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

3 comments

0

Theres a reason why there are variables.

0

Also, did you ever learn about local variables? Dont forget to learn about those, those are very useful.

0

has anyone knew that the second parameter of instance.new is depricated

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