Problem with a touch script

Hello guys !
So I’m trying to make a script that when a platform touch a part, the platform became Invisible.

For this, I made a bool value, “Visibility”. When it is true, the platform is visible and when it’s false, it’s invisible. (This work)

For the part touched it doesn’t work, here is the script:

script.Parent.Touched:Connect(function(hit)
if hit.Name == "MovingPart" then
script.Parent.Parent.MovingPart:WaitForChild("Visibility").Value = false
end
end)

Can you help me ??
Thanks for every answers !

1 Like

There are two possible reason why this wouldn’t work one being that you aren’t changing MovingPart.Transparency when the visibility is changed, and the second being that both parts are non-collidable, thus the touched event never firing.

Addition: you can just set hit.Visibility.Value to false, rather than getting hit via script.Parent.Parent.MovingPart Even better, you can directly modify the Part’s Transparency, via a simply ternary operator

Part.Transparency = (Part.Transparency == 0 and 1 or 0)

You’re missing a ) at the last end…

It needs to be

script.Parent.Touched:Connect(function(hit)

end)

This of course will cause the script to never run since the Syntax is off…

Anytime you have parenthesis, you have to have a pair of them…

Yea, simple syntax error on the last “end”, should be end). recommend using the Output and script analysis windows. These tools would have answered your question. Both windows can be enabled in the “View” menu.

1 Like

I test the MovingPart.Transparency and this work.
I don’t know that when part are can collide = false then the touched doesn’t work

I use these tools. This is only because I write this on my smartphone and I don’t have the script with me (I remember I put “end)” but not on this topic :wink: )

Did you… even try looking at the error console before coming here?

Please click this.

I tried some script before coming here and look for error, any error

Uhh not sure what you just said.

  1. The correct property is “Transparency”
  2. “Visibility” is not a child of part, Transparency is a property. You don’t need to wait for properties.

Did you even read my topic, it’s said that it’s a Visibilty BOOL VALUE, BOOL, like true or false.
This value change the transparency and the can collide of a model

1 Like

I don’t think the .Touched event works on moving parts?

I put two print() to see where it doesn’t work
script.Parent.Touched:Connect(function(hit)
print(1)
if hit.Name == “MovingPart” then
print(2)
script.Parent.Parent.MovingPart:WaitForChild(“Visibility”).Value = false
end
end)

the print(1) work good but not print(2)

What exactly is moving part? Does it move with physics, or CFrame? Is it CanCollide false?

There is two MovingPart, MovingPart (Model) and MovingPart(PrimaryPart) it move with a SetPrimaryCFrame(). I want to make when the MovingPart(PrimaryPart) touch a part the MovingPart(Model) became Invisible and NoCanCollide and vice versa

I don’t think setting the CFrame will fire the Touched event.

Do you know how can I change that ?

I believe :GetTouchingParts() will work. You can make a loop and run :GetTouchingParts() on script.Parent and see if the MovingPart is touching it.

Simple Example
while (true) do
	
	for i, v in pairs(script.Parent:GetTouchingParts()) do
		
		if (v.Name == "MovingPart") then
			
			print("Collision!");
			
		end
		
	end
	
	wait(.1);
	
end
1 Like

Thanks, I finally found what I was searching for !