Touched and TouchEnded

Hello!

I am having some problems with the Touched and TouchEnded functions.
It seems that even though I’m making a debounce, these functions still trigger multiple times.

I’m really looking for a solution to this, as it would help me greatly.

local DoorOpenPart = script.Parent.Obby2PlayerDoorOpenPart
local DetectionPart = script.Parent.DetectionPart
local AnchorPart = script.Parent.AnchorPart

local debounce = false

AnchorPart.Touched:Connect(function()
	if debounce == false then
	debounce = true
		print("In use")
	else
	return end
end)

AnchorPart.TouchEnded:Connect(function()
	print("Not in use")
		debounce = false
end)

I see what the issue could be here, the different parts of the character are touching the door multiple times as they walk through, which would cause it to have issues. I wonder if you could use :GetPartsInPart() on the touchended event to see if the character is still touching the door.

Yes, that sounds like it might be the issue.

Just so I get it right. Should I do the :GetPartsInPart() on the players character or the model?

You need to use it on the model

Thank you for your reply.

I don’t quite see where I should fit this in and how it would work. I’m not that experienced of a scripter, so that might be why. Could you maybe, if you want to, explain further?

Editing off of your script:

local plrsTouching = {} -- tbl for touching plrs
local DoorOpenPart = script.Parent.Obby2PlayerDoorOpenPart
local DetectionPart = script.Parent.DetectionPart
local AnchorPart = script.Parent.AnchorPart

local debounce = false

AnchorPart.Touched:Connect(function(part)
    if game.Players:FindFirstChild(part.Parent.Name) and debounce == false then
        debounce = true 
		plrsTouching[part.Parent.Name] = true -- will create instance in table if not already
        print("active")
    else 
        return nil 
    end
end)

AnchorPart.TouchEnded:Connect(function(part)
	if game.Players:FindFirstChild(part.Parent.Name) then
        local found -- bool for plr detection
        for i,v in pairs(AnchorPart:GetPartsInPart()) do
             if game.Players:FindFirstChild(v.Parent.Name) and v ~= part then
                return nil -- kills function if player is still there
             end
        end
        table.remove(plrsTouching, part.Parent.Name)
        print("deactivated")
	    debounce = false
    end
end)

If you have any issues, let me know!

Hello!

Thank you for this, I will test it out later.

If I get it right, and also so that I understand it.

What this does is it takes the player who touches it and adds the player to a table at the Touched event. Then at the TouchEnded event it finds the player and checks if the player is still touching the part (even if its just a part of the players character) and if the player is still touching it, it returns nil. But if it is not, the player get removed from the table and the function of whatever you want happens.

Please correct me if I’m wrong.
And again, thank you!

Yep, that is correct! It checks all parts that touch it, and only selects the players.

It seems like it works, but there is an issue though.

It can’t seem to remove the players name from the table, because I think it expects the position / number of the player in the table. So when I try to make it remove the players name, it says:

(number expected, got string)

AnchorPart.TouchEnded:Connect(function(part)
    if game.Players:FindFirstChild(part.Parent.Name) then
        local found -- bool for plr detection
        for i, v in pairs(AnchorPart:GetPartsInPart()) do
            if game.Players:FindFirstChild(v.Parent.Name) and v ~= part then
                return nil -- kills function if player is still there
            end
        end
        -- Remove a player from the table by name
        plrsTouching[part.Parent.Name] = nil
        print("deactivated")
        debounce = false
    end
end)

Touched and TouchEnded are basically deprecated, they are so glitchy

ZonePlus is what everyone uses instead, here’s the link

I tried to do the same thing for tweening.

local playerstouching = {}
local DoorOpenPart = script.Parent.Obby2PlayerDoorOpenPart
local DetectionPart = script.Parent.DetectionPart
local AnchorPart = script.Parent.AnchorPart
local EntranceDoor = script.Parent

local debounce = false

local players = game:GetService("Players")

local inUse = false

local cantouchend = false

DetectionPart.Touched:Connect(function(hit)
	if game.Players:FindFirstChild(hit.Parent.Name) and debounce == false and inUse == false then
		cantouchend = false
debounce = true
		inUse = true
		playerstouching[hit.Parent.Name] = true
		local tweenservice = game:GetService("TweenService")
		local player = players:GetPlayerFromCharacter(hit.Parent)
		local tweeninfo = TweenInfo.new(0.4)
		local tween = tweenservice:Create(DoorOpenPart, tweeninfo, {CFrame = DoorOpenPart.CFrame * CFrame.Angles(math.rad(90), 0, 0) + Vector3.new(0, 6.7, -1)})
		tween:Play()
		tween.Completed:Connect(function()
		task.wait(0.5)
		cantouchend = true
	end)
	else
		return nil
	end
end)

DetectionPart.TouchEnded:Connect(function(hit)
	debounce = true
	if game.Players:FindFirstChild(hit.Parent.Name) and cantouchend then
		for i,v in pairs(workspace:GetPartsInPart(DetectionPart)) do
			if game.Players:FindFirstChild(v.Parent.Name) and v ~= hit then
				return nil
			end
		end
	local tweenservice = game:GetService("TweenService")
	local tweeninfo = TweenInfo.new(0.4)
	local tween = tweenservice:Create(DoorOpenPart, tweeninfo, {CFrame = DoorOpenPart.CFrame * CFrame.Angles(math.rad(-90), 0, 0) - Vector3.new(0, 6.7, -1)})
		tween:Play()
		tween.Completed:Connect(function()
			playerstouching[hit.Parent.Name] = nil
			debounce = false
			inUse = false
cantouchend = false
		end)
	end
end)

But it seems like even though I added a task.wait and debounce, sometimes the tween still plays multiple times or interrupt each other.
Do you know a fix for this?