How do i make a function if a specific part is touching it and when it stops touching it?

I want to check to see if a part hits a specific part so I can make a ProximityPrompt hide or show.

It does not seem to start the function when it touchs the part.

I looked at many forms and they are when they touch any object instead of one object.

local ConnectPart = script.Parent.Parent.Parent.GMCCamper.Body.ConnectPart
local hitch = script.Parent.HITCH
local function onPartTouched(hitch)
	print(ConnectPart.Name .. " has touched " .. hitch.Name)
	if ConnectPart.Touched == hitch then
		ProximityPrompt.Enabled = false
	else
		ProximityPrompt.Enabled = true
	
	end
end
local function onPartTouchedStoped(hitch)
	if ConnectPart.TouchEnded == hitch then
		ProximityPrompt.Enabled = false
	else
		ProximityPrompt.Enabled = true
	end
end

	ConnectPart.Touched:Connect(onPartTouched)
	ConnectPart.TouchEnded:Connect(onPartTouchedStoped)
1 Like

Its not working due to you are putting the event .Touched and .TouchEnded as a condition and saying that its euals to another part. The .Touched and .TouchEnded are called when something hit or stopped hitting a part.
Remove those:
if ConnectedPart.Touched == hitch and same for .TouchEnded

Use a condition that detects if its touched by a player by trying something like:
if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then

And then to complete your code you can say:
if hit == Hitch then

(Cant send fixed code, its hard on mobile lol)

So is this for when the player touches the part?

1 Like

As i said, it is because you are trying to detect if a connected event is an instance and it doesnt work. You will need to say if the part who hits the current part is a certain instance.

So it would be:
if hit == hitch then

But know that the given argument passed trough Touched or TouchEnded are the parts that hit the part.
Plus you already assigned hitch as the part who touched your other part, so its just like it detects if the part touch himself.

So you may remplace the ‘hitch’ inside the both function by ‘hit’ instead.

Oh my gosh, I am so stupid I did not realize that I could do it that way tysm!!!

1 Like