Object Collisions - Help

I am trying to script two objects colliding together and causing a huge explosion. I currently have the objects tweened and they meet however, I can’t seem to figure out how I would write the code for the objects to collide. Should I use a remote event? My thought process through this collision is that:

local part1 = script.Parent.Part1
local part2 = script.Parent.Part2

if(part1.Touched == part2) then
      --make an explosion happen and the parts disappear
end

How would I make this happen because I know that the touched statement doesn’t work in the fashion that the code that I wrote above does.

The touched event has a parameter of which part is touching. Here is a code example of what you could do to see when part1 and part2 collide:

part1.Touched:Connect(function(hitPart)
    if hitPart == part2 then
        -- explode
    end
end)
4 Likes

How would the explosion be made? From what I’ve seen in API reference, I don’t see anything that could make the part explode. Would I just make a separate event that is run once the parts collide?

If you want to use a roblox explosion then you could create a new explosion instance. Here would be some code that makes part1 explode when it collides with part2:

part1.Touched:Connect(function(hitPart)
    if hitPart == part2 then
        local explosion = Instance.new("Explosion")
        explosion.Position = part1.Position
        explosion.Parent = workspace -- detonates the explosion
        explosion.BlastPressure = 500000
        explosion.BlastRadius = 10
        part1:Destroy() -- make part1 "vanish"
        part2:Destroy() -- make part2 "vanish"
    end
end)
2 Likes

I realized that right now, but thank you for helping!

You can use the Explosion instance https://developer.roblox.com/en-us/api-reference/class/Explosion.

Also, touched is not the best option, especially if your parts are anchored, perhaps use Region3’s or Magnitude checks. If touched isn’t working properly for you, you can USS what I have suggested

By the way, RBXScriptSignal:connect is deprecated. Use RBXScriptSignal:Connect instead.

3 Likes