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)
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.