✓ Accepted answer
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
end
if cold == true then
Sprinkle.Enabled = true
print("On")
end
if cold == false then
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
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)
5 comments
script.Parent..MouseClick:connect(onClicked) is the fix.
That seemed to have done the trick :). But could you explain why it worked without the ClickDetector part?
Becauuuuuse, the script is inside the ClickDetector. script.Parent references what the script is in, which is the ClickDetector. Saying "script.Parent.ClickDetector" references a ClickDetector within the ClickDetector, which isn't there, so there's your error.
But now i'm wondering why it won't turn off?
Edited