local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.Button1Down:Connect(function()
Fire = true
repeat
script.Parent.Fire:FireServer(Mouse.Hit.p)
wait()
until Mouse.Button1Up
Fire = false
end)
This tutorial has the most simplified version from what I have seen so far of how to achieve a fully automatic gun in one code block.
To explain every frame (HeartBeat) it checks if the mouse is held down and if the gun can fire again via a debounce. If the gun can fire then it will fire one bullet via the function MainModule.cast() then wait() until it can fire again.
Keep in mind it’s not the best method as wait() is capped to 1/30 seconds so it will have a problem with really fast firing guns, the best automatic guns will use an accumulator to make it frame independent which is a bit more complex so just keep this in mind.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
UserInputService.InputBegan:Connect(function(Input, GP)
if GP then
return
end
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
Fire = true
repeat
script.Parent.Fire:FireServer(Mouse.Hit.p)
wait()
until not UserInputService:IsKeyDown(Enum.UserInputType.MouseButton1)
Fire = false
end
end)