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

Projectile stays still?

Asked by 10 years ago

I have been scripting a gun, and while doing so, after making the CFrame of the bullet slightly away from the weapon (to ensure no damage is taken to the player, and for effect), but doing so makes the bullet freeze. I have no idea on how to fix this, but I have isolated the area I think makes it do that. There is also no errors in the output, and the code after it works fine. (In a localscript)

print 'all is good'

local t = script.Parent
local h = t.Handle
local MaxAmmo = script.MaxAmmo
local Ammo = script.Ammo
local AmmoPerClip = script.AmmoPerClip
local s = t.Sight
local running = false
local aiming = false
local AmIEquipped = false

t.Equipped:connect(function()
    AmIEquipped = true
    local plr = game.Players:FindFirstChild(t.Parent.Name)
    local m = plr:GetMouse()
    m.Button1Down:connect(function() --shoot function
        if MaxAmmo.Value == 0 then
            print 'no ammo'
        else
            Ammo.Value = Ammo.Value - 1
            if Ammo == 0 then
                print 'reloading'
                local emag = Instance.new("Part", Workspace)
                emag.FormFactor = "Custom"
                emag.Size = Vector3.new(0.5, 0.8, 0.48)
                emag.Position = h.Position - Vector3.new(0, 1.5, 0)
                MaxAmmo = MaxAmmo - AmmoPerClip
                Ammo = AmmoPerClip
                wait(10)
                emag:remove()
            end
            local b = Instance.new("Part", Workspace)
            b.Name = "Bullet"
            b.BrickColor = BrickColor.new("New Yeller")
            b.FormFactor = "Custom"
            b.Size = Vector3.new(0.05, 0.05, 1.3)
                b.CFrame = h.CFrame * CFrame.new(0, 0, -3) --problem line
            local v = Instance.new("BodyVelocity", b)
            v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            v.velocity = m.Hit.p*50
            b.Anchored = false
            local deb1 = false
            b.Touched:connect(function(hit)
                if not deb then
                    deb = true
                    if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
                        human = hit.Parent:FindFirstChild("Humanoid")
                        if hit.Name == "Head" then
                            human.Health = human.Health - 20
                            print '20'
                        elseif hit.Name == "Torso" then
                            human.Health = human.Health - 15
                            print '15'
                        else
                            human.Health = human.Health - 10
                            print '10'
                        end
                        b:remove()
                        if human.Health == 0 then
                            print 'the human has died'
                        end
                    else
                        print 'an inanimate object got shot! poor thing ;c'
                    end
                end
            end)
        end
    end)
    m.KeyDown:connect(function(key) --run function
        if key == "q" and running == false then
            local char = game.Players:FindFirstChild(t.Parent.Name).Character
            local hum = char.Humanoid
            hum.WalkSpeed = 32
            running = true
        elseif key == "q" and running == true then
            local char = game.Players:FindFirstChild(t.Parent.Name).Character
            local hum = char.Humanoid
            hum.WalkSpeed = 16
            running = false
        elseif key == "r" then
            if MaxAmmo.Value == 0 then
                print 'no ammo'
            else
                print 'reloading'
                local emag = Instance.new("Part", Workspace)
                emag.FormFactor = "Custom"
                emag.Size = Vector3.new(0.5, 0.8, 0.48)
                emag.Position = h.Position - Vector3.new(0, 1.5, 0)
                MaxAmmo = MaxAmmo - AmmoPerClip
                Ammo = AmmoPerClip
                wait(10)
                emag:remove()
            end
        end
    end)
    m.Button2Down:connect(function()
        local cam = game.Workspace.CurrentCamera
        if aiming == false and AmIEquipped == true then
            t.GripPos = Vector3.new(-1, 0, 0)
            print 'aiming'
            cam.CameraType = "Attach"
            cam.CameraSubject = s
            aiming = true
        elseif aiming == true then
            t.GripPos = Vector3.new(0, 0, 0)
            print 'not aiming anymore'
            cam.CameraType = "Custom"
            cam.CameraSubject = plr.Character.Humanoid
            aiming = false
        end
    end)
end)

t.Unequipped:connect(function()
    AmIEquipped = false
end)

2 answers

Log in to vote
0
Answered by 10 years ago

You are only moving the bullet once, while you need it to move constantly. Use a coroutine:

function bM(b)
    while wait(0.01) do
        b.CFrame = h.CFrame * CFrame.new(0, 0, -3)
    end
end
bulletMove = coroutine.wrap(bM);
bulletMove(b); --Call this when you need the bullet to move
0
It seems to stick to the barrel and moves with it, but it doesn't go out and reach a target. lowvillan 10 — 10y
0
Nevermind, I fixed it. Thanks for your help. lowvillan 10 — 10y
0
No problem. PiggyJingles 358 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

Might it be line 41?

Replace line 41 with v.velocity = (m.Hit.p - h.CFrame.p).unit*50 and see if that helps, because the way you had it, the velocity would be the mouse's hit position, instead of the direction to the mouse's hit position.

0
You fixed it, nevermind. :) PsychoWaffle 10 — 10y

Answer this question