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

trying to make a automatic gun with Button1Down but for some reason button1 is always down?

Asked by 7 years ago

so i am trying to make a automatic gun (never tryed making a automatic gun before) but when i try to make it where while Button1Down it shoots every 0.1 seconds it just shoots it constantly idk if im doing it wrong or i need to use another method if so then could you please point me in the right direction? here is my script

player = script.Parent.Parent.Parent -- the player
function fire(v)

    local bullet = game.ReplicatedStorage.bullet:Clone() -- clones a bullet from replicated storage
    bullet.Parent = workspace
    bullet.Velocity = v*1000
    bullet.Position = script.Parent.barrel.Position


    local force = Instance.new("BodyForce") -- makes new body force and puts in inside the bullet this way i can make the bullet drop
    force.force = Vector3.new(0,-5,0)
    force.Parent = bullet





end
------------- this is where im failing at
shooting = false

script.Parent.Unequipped:Connect(function()
    shooting = false

end)

script.Parent.Equipped:Connect(function(mouse)


    if mouse.Button1Down then
        print("down")
        shooting = true
    else 
        print("up")
        shooting = false

    end

    if shooting == true then
        local character = player.Character
        local targetpos = character.Humanoid.TargetPoint
        local lookat = (targetpos - character.Head.Position).unit
        fire(lookat)
        wait(0.1)
        print("fired")
    end

    end)    



1 answer

Log in to vote
0
Answered by 7 years ago

You want to do it like this:

player = script.Parent.Parent.Parent -- the player
function fire(v)

    local bullet = game.ReplicatedStorage.bullet:Clone() -- clones a bullet from replicated storage
    bullet.Parent = workspace
    bullet.Velocity = v*1000
    bullet.Position = script.Parent.barrel.Position


    local force = Instance.new("BodyForce") -- makes new body force and puts in inside the bullet this way i can make the bullet drop
    force.force = Vector3.new(0,-5,0)
    force.Parent = bullet





end
------------- this is where im failing at
shooting = false

script.Parent.Unequipped:Connect(function()
    shooting = false

end)

script.Parent.Equipped:Connect(function(mouse)
mouse.Button1Down:connect(function()
        print("down")
        local character = player.Character
        local targetpos = character.Humanoid.TargetPoint
        local lookat = (targetpos - character.Head.Position).unit
        fire(lookat)
        wait(0.1)
        print("fired")
end)
mouse.Button1Up:connect(function()
        print("up")
end)
end)    




Mouse.Button1Down and Mouse.Button1Up are events like Tool.Equipped is.

0
thank you for you responce but the gun is still isnt automatic popthecorn145 16 — 7y
0
nvm i modifyed it and it works now thanks popthecorn145 16 — 7y
0
You're welcome :D ShadowOfCrimson 27 — 7y
0
Use .Activated, it only fires when the player clicks with tool equipped. hiimgoodpack 2009 — 7y
Ad

Answer this question