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

Debounce not working with touch-activated script?

Asked by
evil29 0
5 years ago

This code is probably way too complex for what should do, but I've been coding for only two days. The goal is to have a part that when touched, turns off all surface lights in the workspace. And then when the touch ends, the lights return to their normal value.

function onTouched(hit)

local descendants = game.Workspace:GetDescendants()
for index, descendant in pairs(descendants) do
    if descendant:IsA("SurfaceLight") then
        descendant.Brightness = 0
        end
    end
end

script.Parent.Touched:connect(onTouched)

function onTouchEnded(hit)

local descendants = game.Workspace:GetDescendants()
for index, descendant in pairs(descendants) do
    if descendant:IsA("SurfaceLight") then
        descendant.Brightness = 1
        end
    end
end

script.Parent.TouchEnded:connect(onTouchEnded)

The first section is obviously for the action of touching the trigger, and the second part for when the touch ends. There is only one trigger in the entire game, which is designed for one person, so I'm not too concerned about making the code more efficient. Maybe a Region3 would work better than an OnTouch? Still unclear.

When I first ran the code, all I had was the first part where the lights turn off. I didn't have any issues, but I needed to turn them back on after the touch ended, so I added the second part. This is when I started getting issues. I added a Print line, and saw that the code was being run over a hundred times a second, so after I did some research, I discovered debounces. I added a debounce code, and it accelerated the errors. No matter what version of debounce I used, it either gave me a lot more errors or broke the script, so I think it absolutely has to do with the second half of the code.

What should I do?

0
where's the debounce megukoo 877 — 5y
0
It's not included because it wasn't working. I used the script provided on the ROBLOX developer website and followed several YouTube tutorials, but they don't work properly evil29 0 — 5y

Answer this question