✓ Accepted answer
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
local otherPart
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)
1 comment
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