Self explainatory.
If there is a possible way to also see if a trail is in the air, tell me, because that's what I'm trying to use this for.
Create a Script On Your Part, And Write This:
script.Parent.Touched:Connect(function(hit) --------- If Player Touches, It Does The Function local humanoid = hit.Parent:FindFirstChild("Humanoid") -------Looks For Humanoid if humanoid then ----- Checks If What Touched Part Is A Player print( "I'm Being Touched") ------ Here Is What You Want To Script, When Part Is Touched
There are 2 ways to go about this. You can either use Touched
and TouchEnded
events to see what parts are touching it or use part:GetTouchingParts()
I would personally use part:GetTouchingParts()
as it's a bit more reliable and a bit more compact
Update: GetTouchingParts() method will only work if you want to make sure there are no overlapping parts. Use the first method
(Both scripts would be parented by the part)
**First Method **
local numberOfPartsTouching = 0 script.Parent.Touched:Connect(function(hit) numberOfPartsTouching = numberOfPartsTouching + 1 end) script.Parent.TouchEnded:Connect(function(hit) numberOfPartsTouching = numberOfPartsTouching - 1 end) while wait() do if numberOfPartsTouching == 0 then --Do some cool stuff B) end end
Second Method Using part:GetTouchingParts()
local p = script.Parent while wait() do if table.getn(p:GetTouchingParts()) == 0 then --Do some cool stuff B) end end
I might have some typos in my script and I apologize for that but if this answer helped you then please accept it so we can both get some rep :)
Before I start, i would like to say that there are many methods to know whether if something is touching something else or not.
arguably the most basic and easiest to use is the Touched
event of the basepart. While easy to use, this isn't really the best way to go about using it, for one, since r15 characters have 16 body parts, any collisions with objects happens a maximum of 16 times every instant, I say that because animations can trigger more touched events , which further messes things up
local part = workspace.Part Part.Touched:Connect(function(hit) print(hit.." hit me") end)
Something else that is pretty easy is the GetTouchingParts() function of baseparts, which gets all parts that are touching something. This method has a caveat however, as things with cancollide set to false always have no "touching parts", that is, the length of the TouchingParts array is 0. However, this is better for timing when compared to the Touched event
local part = workspace.Parent Event.Event:Connect(function() print("part is being touched by ".. table.concat(Part:GetTouchingParts()," ,")) end)
A way some developers process collisions is with rays , or more specifically, the FindPartOnRay function.The reason people use this is because of the empty array which a non-canColliding object puts out when you use its GetTouchingParts function. This caveat of the GetTouchingParts function pretty much rends things like invisible hitboxes and laser beams useless. Alternatively, the FindPartOnRay function doesn't have that caveat, as it doesn't take into account the CanCollide of the part it originated from
local origin = script.Parent.Position local direction = script.Parent.CFrame.lookVector*2 local ray = ray.new(origin,direction) Event.Event:Connect(function() local hitPart,hitPos = workspace:FindPartOnRay(ray) if hitPart and Ray:Distance(hitpose) <= Ray.Size.Y/2 then print("touched by "..hitpart) end end)
Note that using more than one ray is going to make this alot more accurate for something, however, if you are using collisions, say, for a bullet, one ray might be enough.
Also note that the surface normal and material of a part at the point of intersection is also returned by the FindPartOnRay function.
Hopefully this helped