Camera viewing gun shooting where it's looking at

I am trying to make a gun where it doesn’t use a mouse click at part but uses the camera where it’s looking at to fire the gun.

LocalScript
local player = game.Players.LocalPlayer

local Camera = workspace.CurrentCamera.Focus.LookVector

local Type = script.Parent.Config.Type

script.Parent.Activated:Connect(function()

script.Parent.Function:FireServer(Camera.p)

end)

script.Parent.Equipped:Connect(function()

game.ReplicatedStorage.ConnectM6D:FireServer(script.Parent.Hand)

if Type.Value == "Left" then game:GetService("RunService").RenderStepped:connect(function() game.Workspace:FindFirstChild(player.Name)["Left Arm"].LocalTransparencyModifier = 0 end)

elseif Type.Value == "Right" then game:GetService("RunService").RenderStepped:connect(function() game.Workspace:FindFirstChild(player.Name)["Right Arm"].LocalTransparencyModifier = 0 end)

elseif Type.Value == "Both" then game:GetService("RunService").RenderStepped:connect(function() game.Workspace:FindFirstChild(player.Name)["Left Arm"].LocalTransparencyModifier = 0 game.Workspace:FindFirstChild(player.Name)["Right Arm"].LocalTransparencyModifier = 0 end)

char.Head.ToolGrip.Part0 = char.Head

char.Head.ToolGrip.Part1 = script.Parent.Attachment

end

end)

script.Parent.Unequipped:Connect(function()

game.ReplicatedStorage.DisconnectM6D:FireServer()

end)

Server /

local tool = script.Parent

tool.Function.OnServerEvent:Connect(function(player, mouseHit)
			print(mouseHit)
			local ray = Ray.new(tool.Fire.Position, (mouseHit- tool.Fire.Position).unit * 1000)
			local hit, position = game.Workspace:FindPartOnRay(ray, tool.Parent)
			local distance = (position - tool.Fire.Position).magnitude
			local part = Instance.new("Part")
			part.Anchored = true
			part.CanCollide = false
			part.Transparency = 0
			part.BrickColor = BrickColor.new("Mid gray")
			part.Material = Enum.Material.SmoothPlastic
			part.Size = Vector3.new(0.1, 0.1, distance)
			part.CFrame = CFrame.new(position, tool.Fire.Position) * CFrame.new(0, 0, -distance / 2)
			part.Parent = workspace
			tool.Fire.Fire:Play()
	end

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast

I suggest looking into doing a raycast from the CurrentCamera CFrame position on the CFrame’s LookVector, this would look something like this

local camera = workspace.CurrentCamera

local result = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector)
if result then
    -- your raycast had a result and do with that what you will
end
1 Like