When I clicked the button, its not show the light and doesnt change the value. Just wanna ask whats the problem of it. Thanks.
local OnStuff = script.Parent.Parent.Parent.Parent.Yes.Value
local clickDetector = script.Parent.ClickDetector
local debounce = true
-- When someone pushed another push buttons:
while true do
wait()
if OnStuff == true then
script.Parent.Parent.Lights.Red.Transparency = 0
debounce = false
repeat wait() until OnStuff == false
script.Parent.Parent.Lights.Red.Transparency = 1
debounce = true
end
end
-- When someone pushed this push button:
function onClicked()
if debounce == true and OnStuff == false then
script.Parent.Parent.Lights.Red.Transparency = 0
OnStuff = true
debounce = false
repeat wait() until OnStuff == false
script.Parent.Parent.Lights.Red.Transparency = 1
debounce = true
end
end
clickDetector.MouseClick:connect(onClicked)
1: do NOT use while true do loops, nevermind those loops with wait() inside them. Use .Changed events and task.wait() for guaranteed accurate frame timing.
2: When checking boolean values, you can simply do if OnStuff then (check if true) or if not Onstuff then (check if false).
3: You’re referencing to the current value in the first line of your script. You need to remove .Value and append it to all uses of OnStuff (so OnStuff = true becomes OnStuff.Value = true)
4: Use :Connect() instead of :connect(). This rule applies for all other methods or functions that have a lowercase alternative. The lowercase alternatives are deprecated and may function differently than you intend it to.
5: Finally, if you want a really easy ‘toggler’ line of code, instead of doing if OnStuff.Value then OnStuff.Value = false else OnStuff.Value = true, you can simply just do OnStuff.Value = not OnStuff.Value
In terms of major efficiency and maintainability improvements however, you should use script variables instead of value instances, and keep related code in one script if possible. If necessary, make children scripts, but this should really only be done when working with module scripts.
Loading screens when you have a spinner. You can use the deltaTime parameter given by .Heartbeat or other RunService events to add to the rotation of the spinner, instead of using some set value.
Also works for timers with milliseconds displayed. Although task.wait(0.1) is a MUCH better alternative for this now, the same deltaTime could be used for the milliseconds, when arithmetic is applied properly.
You placed your functions BELOW The while loop, meaning your script will never reach those lines to set up the functions.
Im not sure why you need a while loop but just move your functions above the while loop like this :
local OnStuff = script.Parent.Parent.Parent.Parent.Yes.Value
local clickDetector = script.Parent.ClickDetector
local debounce = true
-- When someone pushed this push button:
function onClicked()
if debounce == true and OnStuff == false then
script.Parent.Parent.Lights.Red.Transparency = 0
OnStuff = true
debounce = false
repeat wait() until OnStuff == false
script.Parent.Parent.Lights.Red.Transparency = 1
debounce = true
end
end
clickDetector.MouseClick:connect(onClicked)
-- When someone pushed another push buttons:
while true do
wait()
if OnStuff == true then
script.Parent.Parent.Lights.Red.Transparency = 0
debounce = false
repeat wait() until OnStuff == false
script.Parent.Parent.Lights.Red.Transparency = 1
debounce = true
end
end