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

Debounce affects all buttons rather than the one touched?

Asked by
R_alatch 394 Moderation Voter
6 years ago
Edited 6 years ago

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
0
It's a part... R_alatch 394 — 6y
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????? ._. greatneil80 2647 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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

0
Theres a reason why there are variables. hiimgoodpack 2009 — 6y
0
Also, did you ever learn about local variables? Dont forget to learn about those, those are very useful. hiimgoodpack 2009 — 6y
0
has anyone knew that the second parameter of instance.new is depricated saSlol2436 716 — 6y
Ad

Answer this question