Part not disappearing OnClick

I have been practicing on baseplates to learn Lua in small steps and today I was working on making parts interact on touch and on click. I was able to complete the on touch part however I seem to have written my script badly. I attached a script and a click detector to my part. Then I wrote the script as follows.

     local part = script.Parent --find the part that the script is attached to

part.Size = Vector3.new(50, 30, 5) --changes the part size(experimental purposes)

function onClick()
	if part.ClickDetector == true then 
		part.Transparency = 1 --changes the transparency to make it disappear
		part.CanCollide = false --makes it so you can't collide on the part
	end
end


onClick() --calls the function

That’s my code that I have been using to try to make the block disappear and make untouchable when clicked. It hasn’t been working as you know. I have tried a various amounts of solutions including youtube, devforum, developer hub, and even revewing my code thoroughly many times. Please help me fix my problem…

2 Likes

u do this

script.Parent.ClickDetector.MouseClick:Connect(function()
--add ur code below what you want to happen when they click part
end)
4 Likes

Thank you so much!! I made such a big mistake and I forgot about MouseClick… I knew it involved using the ClickDetector when calling the function but I didn’t remember about MouseClick so I tried to do it with conditionals.

this right here is basically thisimage

script.Parent.ClickDetector.MouseClick:Connect(function()
	part.Transparency = 1
		part.CanCollide = false
end)

So your onClick function is just what you call when the event fires.

The click detector is what throws the event.
Same way you would of connected a part throwing the touched event to your onTouch function.

Each Instance in the game will have certain events and it can gain more from its parent class:
Parts have BasePart events.
They are listed at the bottom of the API page.

This is Event Driven programming, using scripts to reach to changes in the game.