Need some help with this simply code

When I clicked the button, its not show the light and doesnt change the value. Just wanna ask whats the problem of it. Thanks.

ButtonScript_ (MTR5412 Editing) - Roblox Studio 8_11_2022 20_52_28

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.

3 Likes

Also, “while … do” loop can be replaced with game:GetService(“RunService”).Heartbeat

3 Likes

Sorry, I’m not OP, but could you index an example on this?

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.

3 Likes

Documentation page
simple example:

local RunService = game:GetService(“RunService”).Heartbeat

RunService.Heartbeat:Connect(function(Step)
-- code runs every frame 
end)
4 Likes

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


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.