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?