How to make a not touched event

hi im making a script where when a part is not touched the code runs but I havent found a way for it to work

you use wait() so it waits and if the wait is finished it runs

I need to understand how this would work

Oh he deleted his reply I couldnt see his suggestion yet

Use TouchEnded

local part = script.Parent
part.TouchEnded:Connect(function(TouchedPart)
 -- code here
end)
1 Like

The fact I’m only now realising this a thing is killing me :skull:

1 Like

one more question how do you run a touch code until it isnt touched

use touched instead of touchended.

but to make a loop until it isnt touched.

try this

local part = script.Parent
part.Touched:Connect(function(TouchedPart)
while true do
 -- code here
wait() -- loop cooldown
end
end)
local toTouch = workspace.Part1
local touching = workspace.Part2
repeat
   task.wait()
   print('Not touching')
until table.find(toTouch:GetTouchingParts(),touching)
1 Like

how about when a part is touched by another part (both are anchored) how can you detect if one was touching another when the game is ran (they also were put into studio touching each other).

1 Like

The methods mentioned so far sound quite costly in terms of performance. They also have drawbacks, such as the yielding nature of while true do loops.

I would recommend using workspace:GetPartsInPart() to detect physical part interactions. This function is rather new compared to .Touched and doesn’t require a TouchInterest.

To detect if a part is touching another part:

local touching = workspace:GetPartsInPart(workspace.Part)
if #touching >= 1 then
    for _,part in pairs(touching) do
        print(part.Name.." intersects."
    end
end

To detect if no parts are touching:

local touching = workspace:GetPartsInPart(workspace.Part)
if #touching = 0 then
    print("No intersection!")
end

workspace:GetPartsInPart() returns a table (regular array) containing every intersecting part. If the table is empty we know there are no intersecting parts.

This function will return all intersecting parts even if both parts are non-CanCollide and non-CanTouch.

If you want to alter which parts are detected by the function you can use OverlapParams.new(), more information about that can be found on the yellow hypertext I left above.

With this, you can easily detect intersecting parts, but our script only runs the function once. We want it to run on a loop. Like I mentioned earlier: task.wait(), repeat, and while true do lead to poor performance due to their reliance on code timing.

So, instead, we will use RunService Heartbeat.

local RS = game:GetService("RunService")

RS.Heartbeat:Connect(function()
    local touching = workspace:GetPartsInPart(workspace.Part)
    if #touching = 0 then
        print("No intersection!")
    end
end)

The above function will run every frame. Each frame the code will determine whether or not workspace.Part is touching something. And if it is not touching anything we print to the console.

Lastly, I would like to point out that it is more performant to use workspace:GetPartsBoundsInBox() if the part you are using to detect collisions is a box shape. Same goes for workspace:GetPartsBoundsInRadius() if your part is spherical.

I hope this helps! :slight_smile:

You could make it run the code when the part loads but when a player steps on it the script will be disabled

simple example:

script.Parent.Touched:Connect(Function(Hit)
local touched = false

script.Parent.Touched:Connect(function()
	touched = true
end)

repeat
	wait()
	print("test")
until
	touched == true

Repeatedly will run the code under “repeat” until the variable that changes when the part is touched changes.

Anything under the repeat section will loop until the part is touched so do whatever you want :slight_smile:

But just know that if you want to make it so only player’s can touch it you have to include the player in the touch event. for example:

local touched = false

script.Parent.Touched:Connect(function(hit)
	if game.Players:FindFirstChild(hit:FindFirstAncestorWhichIsA("Model").Name) then
		touched = true
	end
end)


repeat
	wait()
	print("test")
until
	touched == true