Ad
Log in to vote
1

A gun I rescripted because bodythrust Is deprecated won't work?

Asked by 4 months ago

So recently BodyThrust Is deprecated thus forcing me to use VectorForce, which Is actually easy to control than BodyThrust (To read about VectorForce use on this link: https://developer.roblox.com/en-us/api-reference/class/VectorForce). So anyways I decied to re-script It so It has VectorForce. The way I do It Is to add a attachment which Is parented In the newBullet (The bullet) and made newBulletThrust Instance to VectorForce, everything works besides the position of the bullet, the bullet appears to work but doesn't go to the right place, It just doesn't go to the attachment of the handle (Yep, In the tool there Is an attachment to the handle which Is places next to the muzzle) It just stays In the same places even when I move, Ill make a video soon but Im going to school soon so anyway here Is the script:

local tool = script.Parent
local pew = tool:WaitForChild("Pew")
local debounce = false

tool.Activated:Connect(function()
    if not debounce then
        debounce = true
        pew:Play()
        local newBullet = Instance.new("Part")
        local newBulletThrust = Instance.new("VectorForce")
        local newBulletAttachment = Instance.new("Attachment")

        newBullet.Parent = game.Workspace
        newBulletThrust.Parent = newBullet
        newBulletAttachment.Parent = newBullet
        newBulletThrust.Attachment0 = newBulletAttachment
        newBulletThrust.Force = Vector3.new(0,0,-10000)
        newBullet.BrickColor = BrickColor.Blue()
        newBullet.Position = script.Parent.Handle.Attachment.Position
        newBullet.Orientation = script.Parent.Handle.Attachment.Orientation
        newBullet.Size = Vector3.new(1,1,1)
        newBullet.Touched:Connect(function(hit)
            local humanoid = hit.Parent:WaitForChild("Humanoid")
            if humanoid then
                humanoid:TakeDamage(20)
            end
        end)
        wait(1)
        newBullet:Destroy()
        wait(1)
        debounce = false
    end
end)

1 answer

Log in to vote
0
Answered by 4 months ago

First, since you're setting the position and orientation of the bullet equal to the attachment you could just use .CFrame, so you can replace lines 19 and 20 with newBullet.CFrame = script.Parent.Handle.Attachment.CFrame. The next thing that I saw in your code was that when you added the event listener the bullet destroy function was outside of it. This means that after you create a listener 1 second later you delete the bullet whether or not the listener was triggered. A fix would be this, replacing line 22 to 31:

newBullet.Touched:Connect(function(hit)
    local humanoid = hit.Parent:WaitForChild("Humanoid")
    if humanoid then
                humanoid:TakeDamage(20)
    end
    wait(1)
        newBullet:Destroy()
end)
wait(1)
debounce = false

I really hope this helped! If it did please mark it as the answer, otherwise just comment if there are any errors. Good Luck!

0
It doesn't work, sorry! imnotaguest1121 89 — 4mo
0
That's ok, were there any errors? strangejmaster43 139 — 4mo
Ad

Answer this question