I am attempting to make a script to Enable and disable particles (there are 24 particle emitters) from a part by clicking a nob, but it's not working. Could you please help? Thanks in advance!
function shower() local nob = script.Parent.HeadandNob.Nob local head = script.Parent.HeadandNob.HEad local pe = head.one, head.two, head.three, head.four, head.five, head.six. head.seven. head.eight. head.nine, head.ten, head.eleven, head.twelve, head.thirteen, head.fourteen, head.fifteen, head.sixteen, head.eighteen, head.nineteen, head.twenty, head.twentyone, head.twentytwo, head.twentythree, head.twentyfour local state = script.Parent.Value if state.Value == false then state.Value = true pe.Enabled = true else state.Value = false pe.Enabled = false end end script.Parent.HeadandNob.Nob.ClickDetector.MouseClick:connect(shower)
On line 4, you're defining pe
with multiple values. This is what arrays are for. Essentially, lines 9 and 13 are only affecting the first value assigned to the variable. Here's an example of how this works:
local blah = "1",2; print(blah) --> 1 (only "1" is assigned to 'blah') local first,second = "1",2; print(first,second) --> 1 2 (a reference was made for both values)
You can take advantage of arrays here. Put all of the Particles in an array, then iterate through the array and change each index's Properties.
local nob = script.Parent.HeadandNob.Nob local head = script.Parent.HeadandNob.HEad --Typo? local pe = {head.one, head.two, head.three, head.four, head.five, head.six. head.seven. head.eight. head.nine, head.ten, head.eleven, head.twelve, head.thirteen, head.fourteen, head.fifteen, head.sixteen, head.eighteen, head.nineteen, head.twenty, head.twentyone, head.twentytwo, head.twentythree, head.twentyfour} --Array local state = script.Parent.Value function changeStateTo(bool) --Parameter: true_or_false. state.Value = bool; for _,v in next,pe do v.Enabled = bool; wait(); end end function shower() if not state.Value then changeStateTo(false); else changeStateTo(true); end end --`connect` is deprecated. script.Parent.HeadandNob.Nob.ClickDetector.MouseClick:Connect(shower)