Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ClickDetector is not a valid member of ClickDetector?

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)

5 comments

0

script.Parent..MouseClick:connect(onClicked) is the fix.

0

That seemed to have done the trick :). But could you explain why it worked without the ClickDetector part?

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.

0

But now i'm wondering why it won't turn off?

0

Edited

2 answers

✓ 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 --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)

2 comments

1

Oh That worked like a charm :). Thank you so much.

0

neat trick lol

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

This discussion is preserved as it was. New answers, comments, and votes are closed.