Simple Train Coupler Debugging Help

The issue I am dealing with is that I cant figure out how to filter out everything BUT the couplers itself since the touch affects everything. I have been drawing blanks, cant find anything useful at all to help nor will anything work for this. Regardless of any checks I have made it still interacts with everything and introducing “Else” makes it automatically go to else no matter what/

local TouchPart = script.Parent
local Clicker = script.Parent.ClickDetector

local Attachment = script.Parent.CouplerAttach
TouchPart.Touched:Connect(function(coupling)
	if script.Parent.IsCoupled.Value == false then
		TouchPart.IsCoupled.Value = true
		script.Parent.IsCoupled.Value = true
		local Rope = Instance.new("RopeConstraint")
		Rope.Parent = TouchPart
		Rope.Length = 2
		Rope.Visible = true
		Rope.Attachment0 = TouchPart.CouplerAttach
		Rope.Attachment1 = coupling.CouplerAttach
		print "been touched"
	end
end)

Clicker.MouseClick:Connect(function(Decouple)
	if IsCouple == true then
		local Rope = TouchPart:FindFirstChild("RopeConstraint")
		if Rope then
			Rope:Destroy()
			IsCouple = false
			TouchPart.Value = "0"
			print "is destroyed"			
		end
	end
end)

I would like for the two couplers to touch and then connect, then be destroyed once clicked. I know I need a debounce but right now I just cannot get this to work for the life of me. I dont need anything fancy, just trying to make a simple train coupler.

If anything right now. It interacts with everything and changes the IsCoupled state effectively breaking the model.

Any help is best help. I have been scratching my head for 2 hours and google being google is no help at all.

1 Like

Sorry if this is all over the place. I am tired and refusing to go to bed until I can get this to work

I would recommend checking that the touching part is actually another coupler. Based on your screenshot of the explorer, you can check if the attachment exists:

TouchPart.Touched:Connect(function(coupling)
	if not coupling:FindFirstChild("CouplerAttach") then 
		return 
	end

	if script.Parent.IsCoupled.Value == false then
		TouchPart.IsCoupled.Value = true
		script.Parent.IsCoupled.Value = true
		...

image

Did what you said and no avail

The output seems to indicitate it is working correctly?

Ah, you are right… I cant read apparently. Standby.

Alright so issue

local TouchPart = script.Parent
local Clicker = script.Parent.ClickDetector

local Attachment = script.Parent.CouplerAttach
TouchPart.Touched:Connect(function(coupling)
	if not coupling:FindFirstChild("CouplerAttach") then
		print("coupler not found")
		return
	end
	
	if script.Parent.IsCoupled.Value == false then
		script.Parent.IsCoupled.Value = true
		print("coupler found")
		local Rope = Instance.new("RopeConstraint")
		Rope.Parent = script.Parent
		Rope.Visible = true
		Rope.Length = 2
		Rope.Attachment0 = script.Parent.CouplerAttach
		Rope.Attachment1 = coupling.CouplerAttach
		
		
		
	end
		
end)

TouchPart.ClickDetector.MouseClick:Connect(function(clicked)
	if not clicked:FindFirstChild("RopeConstraint") then
		print("rope not found")
		return
	end
	if script.Parent.IsCoupled.Value == true then
		script.Parent.Rope:Destroy()
	end
end)

What have I done wrong for this scenarios?

What are the properties of the attachments?

Simple standard rope connected to both couplers. Nothing special about it.

Can you please send the properties of the attachments though? I want to check that you haven’t added any offset to them.

(Is there also an issue where you can’t destroy the coupling?)

The offset is fine. Its just it wont destroy once clicked

Alright. It was unclear what you were saying the issue was from the initial reply. Ill have a look into the destroying not working rq

As you are doing that, I am trying multiple methods on my end

The thing given by ClickDetector.MouseClick is the player, NOT the object. Try replacing clicked with TouchPart within the code, and the parameter given in the function(clicked) to player

I got it working for the most part, just need to make a debounce if anything now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.