I wanted to make a shower to where when you click the button the Particle Emitter that i have in the shower head would be enabled. However when i click the button this comes up in the Output " 19:46:28.539 - ClickDetector is not a valid member of ClickDetector".
Here is the full script.
Sprinkle = workspace.Shower.Shower.ParticleEmitter1 local cold = false function onClicked() if not cold then cold = true end if cold == true then Sprinkle.Enabled = true print("On") end if cold ~= true then Sprinkle.Enabled = false print("Off") end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
Every script is given a variable to represent itself as an object in the hierarchy, called script
. The parent of the script, script.Parent
, is the object which script
is a direct child of. In other words, it tells you what the script is in, such as a ClickDetector. script.Parent
== the ClickDetector that the script is in. So, if you say script.Parent.ClickDetector
, you're trying to access a ClickDetector within that ClickDetector, which isn't there.
On the last line, replace script.Parent.ClickDetector
with script.Parent
.
Now your script does not error, but your shower won't turn off. Let's see why.
Sprinkle = workspace.Shower.Shower.ParticleEmitter1 local cold = false function onClicked() if not cold then cold = true --But it never becomes false if it's true! end if cold == true then Sprinkle.Enabled = true print("On") end if cold == false then --The following code will never happen! Sprinkle.Enabled = false print("Off") end end script.Parent.MouseClick:connect(onClicked)
Since cold never goes back to false, we can't shut the sprinkler off. Let's fix that.
Sprinkle = workspace.Shower.Shower.ParticleEmitter1 local cold = false function onClicked() cold = not cold --This is a trick to toggle a boolean's state between true and false. if cold == true then Sprinkle.Enabled = true print("On") elseif cold == false then Sprinkle.Enabled = false print("Off") end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
Did you insert the clickdetector object in the workspace or the object of the shower head?