Ad
Log in to vote
0

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

Asked by 10 days 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)    



Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

1 answer

Log in to vote
0
Answered by 10 days 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 12 — 10d
0
nvm i modifyed it and it works now thanks popthecorn145 12 — 10d
0
You're welcome :D ShadowOfCrimson 22 — 10d
0
Use .Activated, it only fires when the player clicks with tool equipped. hiimgoodpack 1378 — 9d
Ad

Answer this question



Ad