.Touched:Connect function not firing

I am trying to make a door tween open when you touch a part, so far the part doesn’t even fire the function when it is touched. There is no warnings or errors in output. The trigger part has cancolid off, but that should not effect the touched, because that is on.

Here is my code (in full):

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local TriggerPart = script.Parent
local Door = script.Parent.Parent.DoorA


--//Tween Variables
local DoorTweenTime = TweenInfo.new(3)

--//Closed Variables
local doorClosedPosition = Vector3.new(261.875, 7.75, -50.75)

local ClosedGoal = {}
ClosedGoal.CFrame = doorClosedPosition

local DoorCloseTween = TweenService:Create(Door, DoorTweenTime, ClosedGoal)

--//Open Variables
local doorOpenPosition = Vector3.new(253.875, 7.75, -50.75)

local OpenedGoal = {}
OpenedGoal.CFrame = doorOpenPosition

local DoorOpenTween = TweenService:Create(Door, DoorTweenTime, ClosedGoal)

--//Functions
local function doorOpen()
	print("Open")
	DoorOpenTween:Play()
end

local function doorClose()
	print("Close")
	DoorCloseTween:Play()
end

TriggerPart.Touched:Connect(doorOpen)
TriggerPart.TouchEnded:Connect(doorClose)

image

DoorOpenTween should have OpenedGoal instead of ClosedGoal as it’s third parameter.

Got it too work but it fires both about 7 times, unsure why https://gyazo.com/14245561a7a7d46032b06226f8b1a979

This is because Touched fires whenever any of your limbs touch the part. You could check when one limb is touched then set a boolean to stop the Touched connection from firing then set the boolean back when TouchEnded is fired.

Actually this is a better way

local touchingParts = {}

part.Touched:Connect(function(hit)
	if #touchingParts == 0 then
		-- put your play open door tween code here
	end
	
	table.insert(touchingParts, hit)
end)

part.TouchEnded:Connect(function(hit)
	table.remove(touchingParts, table.find(touchingParts, hit))
	
	if #touchingParts == 0 then
		-- put your play close door tween code here
	end
end)

It makes it so when one limb touches the part, the door opens and when all your limbs leave the part the door closes.

1 Like