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

Gun doesn't fire in game?

Asked by
funyun 958 Moderation Voter
9 years ago

It's a tool in StarterPack running on a server script. Check 3 doesn't print. I'm not sure why.

print("Check 1")

gun = script.Parent
firing = false

bullet = Instance.new("Part")
bullet.BrickColor = BrickColor.new("Bright orange")
bullet.Reflectance = 0.2
bullet.TopSurface = Enum.SurfaceType.Smooth
bullet.BottomSurface = Enum.SurfaceType.Smooth
bullet.FormFactor = Enum.FormFactor.Custom
bullet.Size = Vector3.new(0.2, 0.2, 0.6)

velocity = Instance.new("BodyVelocity", bullet)
velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge)

hashit = Instance.new("BoolValue", bullet)
hashit.Name = "HasHit"

print("Check 2")

function onButton1Down(mouse)
    print("Check 3")
    firing = true

    coroutine.resume(coroutine.create(function()
        print("Check 4")
        while firing == true do
            print("Check 5")
            local newbullet = bullet:Clone()
            newbullet.Parent = workspace
            newbullet.CFrame = CFrame.new(gun.Handle.Position + gun.Handle.CFrame.lookVector * 2, mouse.Hit.p)
            newbullet.BodyVelocity.velocity = mouse.Hit.lookVector * 300

            newbullet.Touched:connect(function(hit)
                if newbullet.HasHit.Value == false then
                    newbullet.HasHit.Value = true
                    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= gun.Parent then hit.Parent.Humanoid:TakeDamage(30) end
                    newbullet.BodyVelocity:Destroy()
                end
            end)

            game:GetService("Debris"):AddItem(newbullet, 6)
            wait(.125)
        end
    end))

    mouse.Button1Up:connect(function() firing = false end)
end

function onEquipped(mouse)
    mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end

gun.Equipped:connect(onEquipped)

Answer this question