Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

ClickDetector is not a valid member of ClickDetector?

Asked by 8 years ago

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)
0
script.Parent..MouseClick:connect(onClicked) is the fix. Lacryma 548 — 8y
0
That seemed to have done the trick :). But could you explain why it worked without the ClickDetector part? purplemetro3421 5 — 8y
0
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. funyun 958 — 8y
0
But now i'm wondering why it won't turn off? purplemetro3421 5 — 8y
0
Edited funyun 958 — 8y

2 answers

Log in to vote
0
Answered by
funyun 958 Moderation Voter
8 years ago

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)
1
Oh That worked like a charm :). Thank you so much. purplemetro3421 5 — 8y
0
neat trick lol User#11440 120 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago

Did you insert the clickdetector object in the workspace or the object of the shower head?

Answer this question