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.
01 | Sprinkle = workspace.Shower.Shower.ParticleEmitter 1 |
02 | local cold = false |
03 |
04 | function onClicked() |
05 | if not cold then |
06 | cold = true |
07 | end |
08 |
09 |
10 | if cold = = true then |
11 | Sprinkle.Enabled = true |
12 | print ( "On" ) |
13 | end |
14 |
15 |
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.
01 | Sprinkle = workspace.Shower.Shower.ParticleEmitter 1 |
02 | local cold = false |
03 |
04 | function onClicked() |
05 | if not cold then |
06 | cold = true --But it never becomes false if it's true! |
07 | end |
08 |
09 |
10 | if cold = = true then |
11 | Sprinkle.Enabled = true |
12 | print ( "On" ) |
13 | end |
14 |
15 |
Since cold never goes back to false, we can't shut the sprinkler off. Let's fix that.
01 | Sprinkle = workspace.Shower.Shower.ParticleEmitter 1 |
02 | local cold = false |
03 |
04 | function onClicked() |
05 | cold = not cold --This is a trick to toggle a boolean's state between true and false. |
06 |
07 | if cold = = true then |
08 | Sprinkle.Enabled = true |
09 | print ( "On" ) |
10 | elseif cold = = false then |
11 | Sprinkle.Enabled = false |
12 | print ( "Off" ) |
13 | end |
14 | end |
15 |
16 |
17 | script.Parent.ClickDetector.MouseClick:connect(onClicked) |
Did you insert the clickdetector object in the workspace or the object of the shower head?