I want a script to check if a part is not touched. Just like the opposite of part.Touched:Connect(function())
Does anyone know how to do this?
part.Touched:Connect(function(t)
if t then print("touched")
end
end)
To detect if a part is not touched, keep this in mind; the .Touched event only fires for BaseParts once they are touched, if a part isn’t touched then the event won’t fire in the first place.
Yes, but I want to know if the part isn’t touched by anything so I can run a function. If I just put a function it will run regardless of the part being touched or not.
You can use:
local Part = -- the part
local Parts = Part:GetTouchingParts()
if not Parts then
print("No Touching Part")
end
Basically this returns and instances touching the part, but if there is nothing touching the part, it returns no instances.
But, if the part has the CanCollide
property set to false, add a touch event to add a touchtransmitter, which allows GetTouchingParts
to work:
local Part = -- the part
Part.Touched:Connect(function() end)
local Parts = Part:GetTouchingParts()
if not Parts then
print("No Touching Part")
end
no it will not
part.Touched:Connect(function(hit)
--stuff here only runs if the part was touched
print("was touched")
end)
the function would only run if the event fires, which only fires when a BasePart is touched
Yes, but I want a function that runs when the event isn’t fired. If I just write a normal function, then it will run even if the event is fired.
I’m not 100% sure on how to do this but one way I would think of doing this is if you have a table with the objects as keys and the value of false on server start or the start of the function. If the part is touched, set the value inside of the table to true. Then when you want to know what wasn’t touched, use a loop, and go through everything and then return each index with a false value.
Since the keys would be actual instances, we could hook up the touched event from the table.
local _TouchTable = { Part = false, AnotherPart = false } -- Use path to parts for keys
for i , v in pairs(_TouchTable) do
if v:IsA("BasePart") then -- just to check if you're automating the table contents
v.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") then -- Add paramaters, like specific parts that can touch
_TouchTable[i] = true
end
end)
end
end
Simple Check Function:
local function getPartsTouched()
local ReturnTable = {}
for i , v in pairs(_TouchTable) do
if v == true then
table.insert(ReturnTable , i) -- Adds the parts to the return table
end
end
return ReturnTable -- Reutrns array of parts that were touched
end
You could use GetTouchingParts()
and check if the table returned is empty, if it’s indeed empty, that means that the part does not come in contact with any of the other parts.
local parts = part:GetTouchingParts()
if #parts == 0 then
print("Not being touched!")
end
And if I want to check if it doesn’t get touched by a certain part?
You’d check if the part you are talking about is not in the table
local avoidedPart = -- some part
local parts = part:GetTouchingParts()
local found = false
for i,v in ipairs(parts) do
if v == avoidedPart then
found = true
end
end
If found
is set to true that means the part is indeed being touched by that specific part, if not then it’s not.
Otherwise:
local avoidedPart = -- some part
local parts = part:GetTouchingParts()
if table.find(parts,avoidedPart) == nil then
print("Not touched by that part!")
end
@Slazai, I’ve already mentioned that in my previous post:
You can use the same method, but you just have to check if that part was not touched:
local Part = -- the part
local SpecifiedPart = -- the unwanted part
local Parts = Part:GetTouchingParts()
if not Parts then
print("No Touching Part and not touching specified part")
elseif Parts then
local InstanceTouched = false
for i, v in pairs(Parts) do
if v == SpecifiedPart then
InstanceTouched = true
end
end
if not InstanceTouched then
print("Not Touching Specified Part")
end
end
Well, I guess @Slazai beat me to it.
local Part = -- the part
local Parts = Part:GetTouchingParts()
if not Parts then
print("No Touching Part")
end
That will never be the case because GetTouchingParts() will not return nil if not parts are touching it, instead it will return an empty table.
Just like there is .Touched
you have a .TouchEnded
.
This script says it all
local part = script.Parent
-- Create a BillboardGui
local bbgui = Instance.new("BillboardGui", part)
bbgui.Size = UDim2.new(0, 200, 0, 50)
bbgui.Adornee = part
bbgui.AlwaysOnTop = true
local tl = Instance.new("TextLabel", bbgui)
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
local numTouchingParts = 0
local function onTouch(part)
print("Touch started: " .. part.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(part)
print("Touch ended: " .. part.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
function idk()
somePart.Touched:Connect(function()
return
end)
--If it was not touched put your code here
end
Use touch ended or constantly check if no parts are touching.
local part = -- Path to the part
local touchingParts = part:GetTouchingParts()
if #touchingParts == 0 then -- check if there is 0 touching parts
-- do something when no parts are touching.
end
EDIT: Oh my, this is an old thread. I just realised it after I saw this and posted my reply
Hey i need a simple event so if a part is no longer touching another part, something happens
The most simple way I can think of is doing
part.TouchEnded:Connect(Function())