Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my gun shoot upwards after the first few shots?

Asked by 4 years ago

I'm making a gun.

The guns works perfectly other then the fact that the gun shoots up into the air after the first few shots.

I absolutely have no idea why.

It seems to aim up when I hold the trigger down, but if I tap the button slowly it doesn't occur as much.

Here are the important parts of the gun what do the damage and stuff.

LOCAL SCRIPT:

script.Parent.GunShootEvent:FireServer(mouse.Hit.p) --/triggers enemy damage on click or on hold.

SCRIPT:

ShootEvent.OnServerEvent:connect(function(player, aimplace)
    local chr = player.Character
    if script.Parent.Parent == chr then
        if AmmoLeft.Value <= 0 then
            print("No ammo left.")
        else
            if allowedFire then
            allowedFire = false
            GunSound:Play()
            if script.Parent.Staters.Mode.Value == "Semi" then
                MakeBullet(aimplace) --/makes enemy have damage.
                AmmoLeft.Value = AmmoLeft.Value - 1
            elseif script.Parent.Staters.Mode.Value == "Burst" then
                MakeBullet(aimplace) --/makes enemy have damage.
                AmmoLeft.Value = AmmoLeft.Value - 1
                wait(0.05)
                MakeBullet(aimplace) --/makes enemy have damage.
                AmmoLeft.Value = AmmoLeft.Value - 1
                wait(0.05)
            elseif script.Parent.Staters.Mode.Value == "Full" then
                MakeBullet(aimplace) --/makes enemy have damage.
                AmmoLeft.Value = AmmoLeft.Value - 1
            end
            allowedFire = true
        end
        end
    end
end)

=========================================================================

local function MakeBullet(Target)
    local HandlePosition = script.Parent.Handle.CFrame + script.Parent.Handle.CFrame:vectorToWorldSpace(Vector3.new(0,0,.3))
    local TargetLookWay = HandlePosition:vectorToWorldSpace(Vector3.new(0,1,0)) * 200
    local TorsoLookWay = (script.Parent.Parent:FindFirstChild("Torso").CFrame.lookVector * Vector3.new(1,0,1)).unit
    local TargetAngle = (TargetLookWay * Vector3.new(1,0,1)).unit
    local Angle = math.acos(TorsoLookWay:Dot(TargetAngle))

    if math.deg(Angle) < 90 then
        TargetLookWay = Target - script.Parent.Handle.Position
        if TargetLookWay.magnitude > range then
            TargetLookWay = TargetLookWay.unit * range
        end
        TargetLookWay = TargetLookWay * 1.1
    end

    local RayRay = Ray.new(HandlePosition.p, TargetLookWay)
    local NewPart, position = game.Workspace:FindPartOnRay(RayRay, script.Parent.Parent)
    if NewPart and NewPart.Parent and NewPart.Parent:FindFirstChild("Humanoid") then
        if NewPart.Name == "Head" then
            NewPart.Parent:FindFirstChild("Humanoid"):TakeDamage(damage * 1.25)
        elseif NewPart.Name == "Left Arm" or NewPart.Name == "Right Arm" or NewPart.Name == "Left Left" or NewPart.Name == "Right Leg"then
            NewPart.Parent:FindFirstChild("Humanoid"):TakeDamage(damage * 0.9)
        else
            NewPart.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
        end
    end 

    if position then
        TargetLookWay = position - HandlePosition.p
    end
end

If you need to know anything else, please ask or if you need to see a video of what is happening, just ask me.

Thanks to anyone who helps!

0
I didn't find anything wrong with that code so I'm assuming there's an invisible part in front of camera or it is trying to shoot player's character's parts if you are in first person... A video in studio would help. Igoralexeymarengobr 365 — 4y
0
ok kittonlover101 201 — 4y
0
Its not first person, but does also happen first person. kittonlover101 201 — 4y

Answer this question