How can I make this gun script Automatic?

Hello!

I am making a FPS game and I couldn’t make my script for a fully automatic weapon.
I tried to make it myself but with my smol brain I was unable to do so…

Could anyone help me?

Here’s the code!

--Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")

--Variables
local remoteEvent = replicatedStorage.ToolRemote
local visualsFolder = game.Workspace.Visuals

local function createVisual(startPositon, direction)
	local center = startPositon + direction / 2
	local distance = direction.Magnitude
	
	local visual = Instance.new("Part")
	visual.Parent = visualsFolder
	visual.Anchored = true
	visual.CanCollide = false
	visual.BrickColor = BrickColor.new("Cyan")
	visual.Material = Enum.Material.Neon
	
	visual.CFrame = CFrame.new(center, startPositon)
	visual.Size = Vector3.new(0.2, 0.2, distance)
	
	debris:AddItem(visual, 0.05)
end


remoteEvent.OnServerEvent:Connect(function(player, tool , startPosition, endPosition)
	local maxRange = tool:GetAttribute("MaxRange")
	local damage = tool:GetAttribute("Damage")
	local headshotDamage = tool:GetAttribute("HeadshotDamage")
	local gunShot = tool.Shoot
	
	gunShot:Play()
	
	local direction = (endPosition - startPosition).Unit * maxRange
	
	local raycast = game.Workspace:Raycast(startPosition, direction)
	
	createVisual(startPosition, direction)
	
	if raycast then
		print(raycast.Instance.Name)
		
		local hitCharacter = raycast.Instance.Parent
		local hitName = raycast.Instance.Name
		local humanoid = hitCharacter:FindFirstChild("Humanoid")
		
		if humanoid then
			if hitCharacter.Name ~= player.Character.Name then
				if hitName == ("Head") then
					humanoid.Health -= headshotDamage
				else
						humanoid.Health -= damage
					
				end
			end
		end
	end
end)

Any and all forms of help are appreciated!

4 Likes

Can you show the script that fires the remote event?

Server script doesn’t do anything, you need to show us the client script.

Sorry I couldn’t get to you guys sooner, Anyways here’s the client script as requested

--Services
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

--Variables
local player = players.LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent
local remoteEvent = replicatedStorage.ToolRemote
local visualsFolder = game.Workspace.Visuals

local toolDebounce = false
local debounceTime = tool:GetAttribute("CoolDown")

mouse.TargetFilter = visualsFolder

tool.Activated:Connect(function()
	if not toolDebounce then
		toolDebounce = true
		
		local startPositon = tool.Start.Position
		local endPosition = mouse.Hit.Position
		
		remoteEvent:FireServer(tool, startPositon, endPosition)
		
		task.wait(debounceTime)
		toolDebounce = false
	end
end)

You could check if the player is holding down the LeftMouseButton with the UserInputService.

--Services
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

--Variables
local player = players.LocalPlayer
local mouse = player:GetMouse()

local tool = script.Parent
local remoteEvent = replicatedStorage.ToolRemote
local visualsFolder = game.Workspace.Visuals

local toolDebounce = false
local debounceTime = tool:GetAttribute("CoolDown")

mouse.TargetFilter = visualsFolder

local HoldingMouseButton = false

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		HoldingMouseButton = true
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		HoldingMouseButton = false
	end
end)

tool.Activated:Connect(function()
	if not toolDebounce then
      if HoldingMouseButton == true then
         while task.wait(debounceTime) do
               local startPositon = tool.Start.Position
	       local endPosition = mouse.Hit.Position
		
		remoteEvent:FireServer(tool, startPositon, endPosition)
         end
else
toolDebounce = true
		
		local startPositon = tool.Start.Position
		local endPosition = mouse.Hit.Position
		
		remoteEvent:FireServer(tool, startPositon, endPosition)
		
		task.wait(debounceTime)
		toolDebounce = false
      end

	end
end)

Sorry for the code formatting.

What this code does: It basically listens for the InputBegan and InputEnded Event on the UserInputService. If one of the Events fires, then the HoldingMouseButton Variable is set either to true or false (based if the Input Began or Ended (doesn’t hold MouseButton anymore)).

Let me know if the code works for you. If you have any questions do not hesitate to reply to my response.

Have a nice day!

It doesn’t work for some reason, I don’t know why. It works the same as before, but it isn’t full auto.

I don’t know if this works :

local fire = false -- new variable!!!!111!

-- we put varible here so we easly can adjust!11!!111
local startPositon = tool.Start.Position 
local endPosition = mouse.Hit.Position

mouse.Button1Down:Connect(function()
  if tool.Parent == player.Character then
  if not toolDebounce then
toolDebounce = true
fire = not fire

while fire do
remoteEvent:FireServer(tool, startPosition, endPosition)
task.wait(debounceTime)
end

toolDebounce = false
end
end
end)

mouse.Button1Up:Connect(function()
  fire = false
end)

This is a script created by me a month ago.