Sticking bullet in player

Hello,

I am trying to make bullets stick in their target. I use this code inside the bullet:

local projectile = script.Parent
local listener = nil

listener = projectile.Touched:Connect(function(hitPart)
    if hitPart.Name ~= "CorePart" and hitPart.Name ~= "Bullet" then
        listener:Disconnect()
        print(hitPart)
        local weld = Instance.new("WeldConstraint")
        weld.Part0 = hitPart
        weld.Part1 = projectile
        weld.Parent = projectile
        wait(1)
        projectile:Destroy()
    end

end)

However this happens: https://imgur.com/Xbxn085

As you can see the collision happens but then the welding snaps the projectile back a little bit. How to fix this?

I’m not sure if this would work but can you swap the Part0 and Part1 of the weld and see if it will get fixed?

Just tried it and it does not fix it.

is the welding done on serverside?
if so, it may happen because there will be a delay between server and client.

I suggest running a RayCast after hit.

local projectile = script.Parent
local listener = nil

local function getIgnore()
    local ignore = {projectile}
    local re ; re = function(p)
        if p then
            for _, c in ipairs(p:GetChildren()) do
                if c.Name == "CorePart" or c.Name == "Bullet" then
                    table.insert(ignore, c)
                end
                re(c)
        end
    end
    return ignore
end

listener = projectile.Touched:Connect(function(hitPart)
    if hitPart.Name ~= "CorePart" and hitPart.Name ~= "Bullet" then
        listener:Disconnect()
        
        local r = Ray.new(
            projectile.Position, ((projectile.CFrame * CFrame.new(0,0,-1)).p - projectile.Position).unit * 20
        )
        local h, p = Workspace:FindPartOnRayWithIgnoreList(r, getIgnore())
        if h then
            projectile.CFrame = (projectile.CFrame - projectile.CFrame.p) + p
            local Weld = Instance.new("Weld")
            Weld.Part0, Weld.Part1 = hitPart, projectile
            Weld.Parent = projectile
        end
        game.Debris:AddItrem(projectile, 1)
    end
end)

Please test before using it.

I dont think that matters, welds connects the parts together no matter how far they are from each other.

weld will never release it’s parts, but it wasn’t the problem.
The problem was the part is welded sometimes goes a bit off. That is shown when the player is moving the arms and it got welded floating

It is indeed done server side. But I am testing in solo play in studio where server and client are the same. I will test and return with the results.

Ok, I just tried this. Unfortunately it does not solve the problem. The bullets all stick in the same position and do not reflect the angle they came from.

Try using a local script to where only the client sees the weld just for good measure. Usually, this is just server side lag and not much can be done to fix this, unless you try messing around with network ownership. Just an idea.

  local projectile = script.Parent
   local listener = nil

 listener = projectile.Touched:Connect(function(hitPart)
    if hitPart.Name ~= "CorePart" and hitPart.Name ~= "Bullet" then
    listener:Disconnect()
    print(hitPart)
    local weld = Instance.new("WeldConstraint")
    weld.Part0 = hitPart
    weld.Part1 = projectile
    weld.Parent = projectile
    weld.C0 = hitPart.CFrame:Inverse()
    weld.C1 = projectile.CFrame:Inverse()
    wait(1)
    projectile:Destroy()
end

end)

EDIT: Oops, I didn’t mean to reply to you Desired :joy: This was meant to go on the thread directly.

1 Like

This may not be of any help, but I’ve experienced this a lot but with Anchoring unanchored parts on impact, etc, and it may only look that way in studios for some reason. But if you launch up play, I find that it actually happens less often than it does in studios. Idk if that’s a bug or anything.