Detecting when a player walks off of touched part

Is there an easy way in which i can revert the changes i did to a pressure button after i am no longer contacting it with my avatar

local s = script.Parent
Debounce = false

s.Touched:Connect(function(hit)
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	
	if humanoid ~= nil and Debounce == false then
		Debounce = true
		
		s.Click:Play()
		s.CFrame = s.CFrame * CFrame.new(0,-.1,0)
		
	end
end)
1 Like

You can use BasePart.TouchEnded. Roblox already made it, so why not use it?

1 Like

I tried this but it makes me have to script 2 functions which makes it clunky and one thing that I have learned from roblox studio scripting is that there is always a better way which is less clunky.

Well I suggest adding a separate debounce system for it, which should work somewhat less clunky.

There is a better way, if you’d bother to script it. You can use the Workspace to detect if a player is in the Region3 of your area, but I don’t recommend this as it is slightly slower and will be on loop (or only when the character touches it), assuming you have multiple pressure plates this will cause a lack of server performance.

1 Like

Do not create new topics especially active topics. Creating new topics does not mean you get faster answers.

Anyways, just do what everyone else is saying add a debounce.

1 Like

Its a completely different topic, with similar traits however its more specific to one of my problems it has nothing to do with “Getting answers faster.”

It’s the same problem though, is it not? Spamming the forums with “similar traits” topics is not good practice.

1 Like

Last i checked spamming is posting the exact same copy pasted topic more than twice? Anyway i would appreciate it if you would actually help me instead of trying to argue.

It’s just a little tip of advice. Your choice to learn from the experience or not. Anyways…
I recommend using this module the hit detection is more accurate.

1 Like

sometimes having 2 functions is okay, yknow. ;-;

1 Like

I do try to learn from experience but some times i hit walls and i cannot find easy explanations so i come here for help thanks for the help.

1 Like

I know but i prefer stuff that is easier to read and understand, because I have 50iq and my brain gets distracted easily.

Well I think this would be good to get used to–being comfortable looking at 2 functions. The TouchEnded is already there for you to use so just use it and hopefully you grow more familiar with the whole structure and everything.

1 Like

If you don’t want to use touched and touchended, you could constantly check the touching parts.


local Part =  -- Your part here

local RunService = game:GetService("RunService")

local Pressed = false -- to check if already being pressed
local Step = os.clock()
local DelayTime = 0.1 -- how long between each check

RunService.Heartbeat:Connect(function()
	if os.clock() - Step >= DelayTime then -- if new time - last time checked is > than the DelayTime then continue
		Step = os.clock() -- set the last checked time to current time
		
		local TouchingParts = workspace:GetPartsInPart(Part) -- gets a table of all touching parts
		local PlayerWithin = false -- if already found player in table
		
		for _,Hit in pairs(TouchingParts) do -- get all the parts (Hit) in the table		
			if PlayerWithin == false and (Hit and Hit.Parent and Hit.Parent:FindFirstChildOfClass("Humanoid")) then -- if PlayerWithin is not true (no point in checking if player is already found) and hit is a player then
				PlayerWithin = true -- changing the variable to true
			end
		end
		
		if PlayerWithin == true and Pressed == false then -- if this time we detected a player and last time we didnt, that means a player just started touching the part
			Pressed = true
			print("Press")
		elseif PlayerWithin == false and Pressed == true then -- if this time we wont detect a player and last time we did, the player just stopped touching the part
			Pressed = false
			print("unpress")
		end
		
	end
end)

For this to work make sure your parts thats been touched has CanCollide as false. This is just so if the player is walking while touching it, the animation may make the legs go above the part making it think the player stopped touching the part.

3 Likes