How do i make this raycast script automatic? (gun)

Hello so i got a script for an ak in my game:

local dangerousLaser = script.Parent
local weapon = script.Parent
local canreload = true
local getMouseRemote = game.ReplicatedStorage.GetMousePosition
local uis = game:GetService("MarketplaceService")
dangerousLaser.Equipped:Connect(function()
dangerousLaser.Activated:Connect(function()
	local player = game.Players:FindFirstChild(dangerousLaser.Parent.Name)
	local rayOrigin = dangerousLaser.Handle.rayOrigin.Position
	
	local mousePosition = getMouseRemote:InvokeClient(player)
	local directionToFire = (mousePosition - rayOrigin).Unit * 500
	
	local raycastResult = workspace:Raycast(rayOrigin, directionToFire)
	
	if raycastResult then
		if script.Parent.Handle:FindFirstChild("Ammo").Value > 0 then
			print(raycastResult.Instance.Name)
			script.Parent["Meshes/AK47_m_magrel"]:FindFirstChild("Gun Shot"):Play()
			script.Parent.Handle:FindFirstChild("Ammo").Value = script.Parent.Handle:FindFirstChild("Ammo").Value - 1
			script.Parent.Light:FindFirstChild("PointLight").Enabled = true
			script.Parent.Light:FindFirstChild("BillboardGui").Enabled = true
			script.Parent.Light:FindFirstChild("SurfaceGui").Enabled = true
			print(script.Parent.Handle:FindFirstChild("Ammo").Value)
			local hum = raycastResult.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
			wait(.1)
			script.Parent.Light:FindFirstChild("PointLight").Enabled = false
			script.Parent.Light:FindFirstChild("BillboardGui").Enabled = false
			script.Parent.Light:FindFirstChild("SurfaceGui").Enabled = false
			if hum then
				if raycastResult.Instance.Parent.Name ~= player.Name then
					local blood = game.ReplicatedStorage.Blood:Clone()
					blood.Parent = raycastResult.Instance
					blood:Emit()
					hum:TakeDamage(math.random(13, 15))
				end
			end
		elseif script.Parent.Handle:FindFirstChild("Ammo").Value == 0 then
			if canreload == true then
				canreload = false
				script.Parent["Meshes/AK47_m_magrel"]:FindFirstChild("clip_empty"):Play()
				script.Parent["Meshes/AK47_m_magrel"]:FindFirstChild("AK47 Reload"):Play()
				weapon:FindFirstChild("Reload"):FireClient(player)
				weapon["Meshes/AK47_m_mag"].Transparency = 1
				script.Parent["Meshes/AK47_m_magrel"]:FindFirstChild("AK47 Reload").Ended:Wait()
				weapon["Meshes/AK47_m_mag"].Transparency = 0
				script.Parent.Handle:FindFirstChild("Ammo").Value = 32
				wait(2)
				canreload = true
				end
			end
		end
	end)
end)

and it’s semi-automatic, which i don’t want because it looks like i was lazy, so i wanted to make the AK automatic, but the problem is that i got ZERO idea how would i make it so when the player holds the mouse button, the rifle keeps on firing until its out of ammo or until the player stops holding the mouse, please help!

1 Like