I need this script to be constantly checking if the value is true

Hello, I have this script and I need it to be constantly checking if the value is true and if it is it will spin the parts exc. and also it needs to turn off if the value is false. Here is the script:

sp=script.Parent


bulb=sp:WaitForChild("Bulb")
part1=sp:WaitForChild("Part1")
light1=part1:WaitForChild("SpotLight")
part2=sp:WaitForChild("Part2")
light2=part2:WaitForChild("SpotLight")
toggle=sp:WaitForChild("Toggle")


function updatetoggle()
	bulb.BrickColor=(toggle.Value and BrickColor.new("Really red")) or BrickColor.new("Mid gray")
	light1.Enabled=toggle.Value
	light2.Enabled=toggle.Value
end


toggle.Changed:connect(updatetoggle)
updatetoggle()





rate=1/30
rps=1
currentrotation=math.random()

while true do
	wait(.1)
	while toggle.Value do
		wait(rate)
		currentrotation=currentrotation+(rps*rate)
		part1.CFrame=((bulb.CFrame*CFrame.Angles(0,math.pi*2*currentrotation,0))*CFrame.Angles(0,0,math.pi*.5))*CFrame.new(0,0,part1.Size.z*.5)
		part2.CFrame=((bulb.CFrame*CFrame.Angles(0,math.pi*2*(currentrotation+.5),0))*CFrame.Angles(0,0,math.pi*.5))*CFrame.new(0,0,part2.Size.z*.5)
	end
end

What is the question?

You can use this on a value… value:GetPropertyChangedSignal(“Value”):Connect(function()
Whatever in here
End)

Can you rewrite the script for me im a beginner i don’t really know how to do so.

I’ll try to but I’m not sure exactly what the script is trying to do… maybe lmk that I guess?

Maybe this’ll work (Idk tho)

sp=script.Parent

bulb=sp:WaitForChild(“Bulb”)
part1=sp:WaitForChild(“Part1”)
light1=part1:WaitForChild(“SpotLight”)
part2=sp:WaitForChild(“Part2”)
light2=part2:WaitForChild(“SpotLight”)
toggle=sp:WaitForChild(“Toggle”)

function updatetoggle()
bulb.BrickColor=(toggle.Value and BrickColor.new(“Really red”)) or BrickColor.new(“Mid gray”)
light1.Enabled=toggle.Value
light2.Enabled=toggle.Value
end

toggle:GetPropertyChangedSignal(“Value”):connect(updatetoggle)
updatetoggle()

rate=1/30
rps=1
currentrotation=math.random()

while true do
wait(.1)
while toggle.Value do
wait(rate)
currentrotation=currentrotation+(rpsrate)
part1.CFrame=((bulb.CFrame
CFrame.Angles(0,math.pi2currentrotation,0))CFrame.Angles(0,0,math.pi.5))CFrame.new(0,0,part1.Size.z.5)
part2.CFrame=((bulb.CFrameCFrame.Angles(0,math.pi2*(currentrotation+.5),0))CFrame.Angles(0,0,math.pi.5))CFrame.new(0,0,part2.Size.z.5)
end
end

You can do toggle:GetPropertyChangedSignal and choose which property the event will fire for. But if you like literally want to be constantly checking if a value is true, you can use a while loop condition.

Example 1:

toggle:GetPropertyChangedSignal("PropertyName"):Connect(function()
if toggle == true then
print("True")
end
end)

Example 2:

while toggle == true do
print("True")
task.wait() -- This is to stop the script from crashing as while loops run so fast
end

I suggest not using :wait as it’s deprecated. task.wait() is the new version and it works better if you didn’t know. Also I think you could use less while loops as it’s using up quite alot of resources. There is no need for a while true do loop and also a while toggleValue do loop aswell. You can just do while toggle == true do which will ensure the loop will only run if the toggle value is true but it will also constantly check the conditions of toggle.

Alright thanks man it really helped ill try to fix the script using this