What do you want to achieve? I want to make automatic gun(when you hold left click it automatically shots)
What is the issue? When i hold left click it shot one bullet and no more
What solutions have you tried so far? Did you look for solutions on the Developer Hub? Yes
script.Parent.Equipped:Connect(function(mouse)
game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
gui = script.GunInfo:Clone()
gui.Parent = game.Players.LocalPlayer.PlayerGui
gui.GunName:FindFirstChild("Name").Text = script.Parent.Settings:WaitForChild("Name").Value
gui.Info.Ammo.Text = tostring(script.Parent.Settings.Ammo.Value).."/"..tostring(script.Parent.Settings.MaxAmmo.Value)
mouse.Button1Down:Connect(function()
if script.Parent.Settings.Ammo.Value == 0 or script.Parent.Settings.Ammo.Value < 0 then return end
if script.Parent.Settings.Automatic then
repeat
script.Parent.Events.Shot:FireServer(script.Parent.GunHol.CFrame.Position, mouse.Hit.Position)
wait(0.05)
gui.Info.Ammo.Text = tostring(script.Parent.Settings.Ammo.Value).."/"..tostring(script.Parent.Settings.MaxAmmo.Value)
wait(script.Parent.Settings.DelayOfShoot)
until script.Parent.Settings.Ammo.Value == 0 or script.Parent.Settings.Ammo.Value < 0 or ShootingEnded()
else
script.Parent.Events.Shot:FireServer(script.Parent.GunHol.CFrame.Position, mouse.Hit.Position)
wait(0.05)
gui.Info.Ammo.Text = tostring(script.Parent.Settings.Ammo.Value).."/"..tostring(script.Parent.Settings.MaxAmmo.Value)
end
end)
end)
EDIT: I forgot to post “ShootingEnded” function, here:
function ShootingEnded()
local input = userinput.InputEnded:Wait()
if input.UserInputType == Enum.UserInputType.MouseButton1 then
return true
else
return false
end
end
You can use a boolean value to detect when the player is no longer holding down the mouse. Because you only have to detect when they stop pressing the mouse, you can put this connect event inside of the MouseButton1Down function and disconnect it later.
So your code can look like this:
local Player = game:GetService("Players").LocalPlayer
local Gun = script.Parent
local GunHol = Gun.GunHol
local Shot = Gun.Events.Shot
local Name = Gun.Settings.Name
local Ammo = Gun.Settings.Ammo
local MaxAmmo = Gun.Settings.MaxAmmo
local Automatic = Gun.Settings.Automatic
local ShootDelay = Gun.Settings.DelayOfShoot
Gun.Equipped:Connect(function(mouse)
if Player.Character then
local humanoid = Player.Character:FindFirstChildWhichIsA("Humanoid")
if humanoid and (humanoid.Health <= 0) then
return
elseif not humanoid then
return
end
else
return
end
local relativeCamFrame = Player.Character.Head.CFrame:ToObjectSpace(workspace.CurrentCamera.CFrame)
local cameraMode = Player.CameraMode
Player.CameraMode = Enum.CameraMode.LockFirstPerson
local gui = script.GunInfo:Clone()
gui.Parent = Player.PlayerGui
gui.Enabled = true
gui.GunName:FindFirstChild("Name").Text = Name.Value
gui.Info.Ammo.Text = ("%i/%i"):format(Ammo.Value, MaxAmmo.Value)
local isShooting = false
local shootingConnection = mouse.Button1Down:Connect(function()
if Ammo.Value <= 0 then return end
isShooting = true
if Automatic.Value then
local upConnection = mouse.Button1Up:Connect(function()
upConnection:Disconnect()
isShooting = false
end)
while Ammo.Value > 0 and isShooting do
Shot:FireServer(GunHol.Position, mouse.Hit.Position)
task.wait(ShootDelay.Value)
end
if upConnection.Connected then
upConnection:Disconnect()
isShooting = false
end
else
Shot:FireServer(GunHol.Position, mouse.Hit.Position)
end
end)
local ammoChanged = Ammo:GetPropertyChangedSignal("Value"):Connect(function()
gui.Info.Ammo.Text = ("%i/%i"):format(Ammo.Value, MaxAmmo.Value)
end)
local unequipped = Gun.Unequipped:Connect(function()
unequipped:Disconnect()
ammoChanged:Disconnect()
shootingConnection:Disconnect()
isShooting = false
gui:Destroy()
Player.CameraMode = cameraMode
workspace.CurrentCamera.CFrame = Player.Character.Head.CFrame:ToWorldSpace(relativeCamFrame)
end)
end)