Help my C4 to get stick properly

Hi, I am making a C4. Everything is done except for it to get stick to the walls/players. Well, when the C4 is touched, It should weld to the hit part, which it does, but gets back to where it was. It’s shown in the video:
https://streamable.com/xeyugd
Here’s my code:

 local con;



    con = newC4.Main.Touched:Connect(function(hit)
        if not hit:IsDescendantOf(newC4) and not hit:IsDescendantOf(plr.Character) then
            
            wait()
            
            local WELD = Instance.new("WeldConstraint")
            WELD.Part0 = newC4.Main
            WELD.Part1 = hit
            WELD.Parent = hit
            
            con:Disconnect()

        end
    end)

Also, all the elements of C4 are welded to its main part. As you can see, it’s newC4.Main.Touched.

I gave as much information as I could. Help is very appreciated!:slight_smile:

1 Like

I think you should weld it not when it hits an object but by using RayCasting.

2 Likes

How do I detect when it hits using RayCasting?

A ray is a line with a single endpoint that extends limitlessly in a direction. The purpose of raycasting is to detect if the ray intersects with a part. Unlike Signal Touched, raycasting does not rely on physics.

workspace:Raycast(Vector3 Origin, Vector3 Direction, optional RaycastParams)

For example, you created a script that allows you to cast beams of light, it works but they can go pass through walls, and you don’t want that. Raycasting solves this situation.

Origin is where the single endpoint starts at in 3D world space, and Direction is where the ray can extend.

local result = workspace:Raycast(Vector3.new(0, 10, 0), Vector3.new(0, -50, 0))

WorldRoot:Raycast returns a RaycastResult, which consists of four properties.

Properties

Instance RaycastResult.Instance
The BasePart or Terrain cell that the ray intersected.

Vector3 RaycastResult.Position
The world space point at which the intersection occurred, usually a point directly on the surface of the instance.

Material RaycastResult.Material
The Material at the intersection point. For normal parts this is the BasePart.Material ; for Terrain this can vary depending on terrain data.

Vector3 RaycastResult.Normal
The normal vector of the intersected face.

Sometimes, WorldRoot:Raycast returns nil. This means the ray did not intersect with any objects.

You can read more here.

I tried weld constraint since I do not not what to use other than weld constraint. But do not use it since it would make a player not be able to move if they get hit by it.