Help with loop script

I am making a gun sprinkler like this:

I have a bool value and a script that checks every 1 second if the Bool “tgd/toggled” is enabled.
When it detects that its enabled, the hose and such turns on, however, it won’t detect it turning off, I might be stupid and it might be a really simple fix, so any help is appreciated.

Code:

local tgd = script.Parent.Toggled

local hinges = script.Parent.Parent.HingesOnSprayer
local nzl = script.Parent.Parent.NozzlePart

while true do
	if tgd.Value == true then
		nzl.Nozzle.Muffled.Enabled = true
		nzl.Nozzle.Straight.Enabled = true
		while true do
			nzl.Nozzle.SplashFX:Emit(25)
			hinges.Hit.TargetAngle = -30
			nzl.Nozzle.Hit.Playing = true
			wait(0.5)
			print("Hit")
			hinges.Hit.TargetAngle = 15	
			wait(0.5)
		end
	else
		nzl.Nozzle.Muffled.Enabled = false
		nzl.Nozzle.Straight.Enabled = false
		nzl.Nozzle.Main.Playing = false
		hinges.Hit.TargetAngle = 15
	end
	print("checked")
	wait(1)
end
2 Likes

i dont see it being set to false anywhere in this script, show the code that does that.

and make sure to check for errors and utilize :WaitForChild() if this is being done locally on the client.

1 Like

I am still going to be making a pump with the buttons to enable and disable it, however right now I’m only using the Server sided view to enable and disable it

1 Like

oh i see, its your nested while loop

while true do
nzl.Nozzle.SplashFX:Emit(25)

change true to Toggle.Value, that’ll stop the infinite loop, and allow the main loop to proceed with the if/else checks.

(make sure Toggle.Value is being set to false somewhere)

in the future i would get rid of the main loop as well, considering the sprinkler will be activated/deactivated with a button or some event.

1 Like

so basically change while true do to while Toggle.Value == true do?

might be an insanely stupid question

1 Like

Nevermind, I got it! Thank you sir!

1 Like

yup, glad you got it going now. :+1:

1 Like

why u using while true loop? why not use tgd:GetPropertyChangedSignal(“Value”):Connect() or something like that?

I didn’t know that was a thing until now…

1 Like