Hello Devforum! I just want to know if it is possible to put a condition to a touched event. Like, if part.Touched then
because I am working on a game that if a part is touched, something happens, but when it isn’t touched something still happens just different. I am pretty sure that you can’t if an event, but is it possible to put a condition on it?
Nevermind! You can use part:GetTouchingParts() edit: if it returns “nil” then it means no parts are touching
reference / script from @Slazai
local parts = part:GetTouchingParts()
if #parts == 0 then
print(“Not being touched!”)
end
Not too sure if that would work, but I do recommend checking for the parts property:
player.HumanoidRootPart.Touched:Connect(function(part)
if part.Name == “(name)” then
–(script)
else
–(script)
end
end)
your code won’t work. He is trying to check if a part ISN’T touching another part. Your code only runs when it is touched. The code that i referred to in another reply above is functional and would fix his issue. (hopefully)
local part = workspace.YourPart
part.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
--A player touched that part
end
end)
Or you can do
local part = workspace.YourPart
local t = part:GetTouchingParts()
for i, v in pairs(t) do
if game.Players:GetPlayerFromCharacter(v.Parent) then
--PlayerTouched
end
end
Eh, I don’t see how it wont work, if you know how to change/fix codes you would understand and change mines to something like:
while true do
wait()
script.parent.Touched:Connect(function(part)
if part.Name == “(name)” then
–(script)
else
–(script)
end
end
Sure you might need to paste it in every part and it might cause lag and such but the important thing is that it works.
So, your code says if the part is being touched by “part” then … else if it is touching something else (still touching something just not that specific part) do …
What HonestJar was looking for was how to check if a part is not touching anything! So no, that method doesn’t work. Hope my explanation made you understand
There are two ways to do it.
One way is to use the GetTouchingParts() then table.find to check if parts are touching.
Or if you only need to keep track of 2 parts the I recommend using Part.Touched and storing the touched part in a variable
local TouchingBaseplate = false
Part.Touched:Connect(function(otherPart)
if otherPart == Baseplate then
TouchingBaseplate = true
end
end)
-- Simply invert for TouchEnded
-- To check
if TouchingBaseplate then
end
Ur dam right omg, i didnt see.
Welp it could just go like this:
(scraped)
ye I was going to type wut @koziahss typed
Do you want the event to constantly fire? Or only fire once?
If you want it to constantly fire do this
local part = workspace.Part
part.Touched:Connect(function()
local t = part:GetTouchingParts()
for i, v in pairs(t) do
if game.Players:GetPlayerFromCharacter(v.Parent) then
--PlayerTouched
end
end
end