Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i detect when two parts are colliding?

Asked by 8 years ago
Edited 8 years ago

I can't find any specific function, only i got is a complex algorithm which doesn't fit for me.

Thanks for read

Edit: So far i came up with this

wait(1)

local Oil = game.Workspace:FindFirstChild("OilBlock2"):clone()
local part = script.Parent
local base = part.Parent.Base

function onTouched(hit)
    local check = hit:findFirstChild("Oilblock2")


    if check ~= nil then 
        print("yes oil")

    else print("not Oil")
        end 


end

part.Touched:connect(onTouched)

But either if its OilBlock2 or not, the output is always "not Oil".

0
Ok, the code works but i didn't took in mind that when testing the code the part with the script was also touching the baseplate. Silly me. Thanks for your answers jonathan3576 12 — 8y

2 answers

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

There are a couple different ways you could go about this, and it depends on your definition of a collision.

If a collision is when two parts are intersecting you could use the GetTouchingParts method to determine which parts intersect another. You could then search the table for to see if the desired part is intersecting. Note that this does not count objects that have their CanCollide property set to false.

local part
local otherPart
local colliding = false

local function checkCollision(part, otherPart)
    for _, part in ipairs (part:GetTouchingParts()) do
        if part == otherPart then
             return true
        end
    end
    return false
end

part.Changed:connect(function()
    checkCollision(part, otherPart)
end)

otherPart.Changed:connect(function()
    colliding = checkCollision(otherPart, part)
end)

If a collision includes when two parts are touching but not intersecting (no space between faces, but not intersecting), then we can use the Touched and TouchEnded events. Be wary though, as if two parts are already touching when the game starts, the Touched events will not fire.

local part -- A part to detect touch events on
local otherPart -- The part to check for collisions
local colliding = false

part.Touched:connect(function(hit)
    if hit == otherPart then
        colliding = true
    end
end)

part.TouchEnded:connect(function(hit)
    if hit == otherPart then
        colliding = false
    end
end)

Note that neither of these methods are overly efficient, and would not rely heavily on them. If you are simply checking for collision of these two parts, go ahead. But I would not suggest using this as a custom collision detection engine (nor would I suggest making such a thing in Roblox)

Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Just check if the classname of "hit" is a part.

script.Parent.Touched:connect(function(hit)
if hit.Parent.ClassName == "Part" then
    print("another brick touched!")
    end
end)

Answer this question