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

How Do You Fix Function Firing Twice Or More?

Asked by 3 years ago

Hello, I been Trying to make a NeonParty Or something like that

-- The Code --

local ClickDetector = script.Parent.ClickDetector
local Debounce = script.Parent.Parent.Debounce
local NeonParts = game.Workspace.NeonParts

local Tween = game:GetService("TweenService")

local Info = TweenInfo.new(0.3,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)

local Goal = {
    Color = Color3.fromRGB(91,93,105)
}

ClickDetector.MouseClick:Connect(function()
    if Debounce.Value == false then
        Debounce.Value = true

        local NeonPartsChildren = NeonParts:GetChildren()
        local Count = #NeonPartsChildren

        for i,v in pairs(NeonParts:GetChildren()) do
            wait() 
            local CurrentPart = NeonParts:FindFirstChild(i)

            CurrentPart:GetPropertyChangedSignal("BrickColor"):Connect(function()
                if CurrentPart.BrickColor == BrickColor.new("Really red") then
                    local NeonTween = Tween:Create(CurrentPart,Info,Goal)

                    NeonTween:Play()

                    print("WHY")
                end
            end)

            if CurrentPart.BrickColor == BrickColor.new("Smoky grey") then
                CurrentPart.BrickColor = BrickColor.new("Really red")
            end


            if i > Count - 1 then
                if Debounce.Value == true then
                    Debounce.Value = false
                end
            end
        end    
    end
end)

-- Problems--

This is the script, lets only focus inside the click detector function, when i click, it printed "WHY" 78 Times, Which is normal, I cleared the output then proceed to my second click on the part, But now, it printed TWICE, i cleared the output again, now it printed Thrice and so on, it lags the game everytime i click, Please i need help since i was stuck for 3 hrs for this bug , i tried to find every possible way ;-; ,

-- Details --

--[[ Also, "NeonParts" Is a folder Full of bricks, Every bricks contain number from 1 - 24, since theres 24 bricks

-- [[ The ClickDetectorPart Is Also Seperated From "NeonParts" Folder, Ofc


Please Whelp, Thanks in Advanced!

0
Make sure debounce is a boolean value NathanBlox_Studios 212 — 3y
0
it is a boolean Minecrafter09031031 16 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Do not make debounced an external value, create a variable in the script e.g local db = false, unless you are using it for more scripts

If that does not work add a wait function

Ad

Answer this question