Having problems with getting mouse direction and firing bullet in that direction

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Make a gun that fires a part called bullet. The bullet comes from the handle and is aimed at the mouse direction.

  2. What is the issue? I keep getting the error “ServerScriptService.ShootReceiver:20: attempt to perform arithmetic (sub) on Instance and Vector3”

If you need the client script please let me know as im new to posting on devforum.

-- VARIABLES --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Shoot = ReplicatedStorage:WaitForChild("Shoot")
local gun = game.Workspace:WaitForChild("Glock")
local bullet = ReplicatedStorage:WaitForChild("Bullet")
local handle = gun:WaitForChild("Handle").Position
local ExitPoint = gun:WaitForChild("ExitPoint").Position

-- MAIN --
Shoot.OnServerEvent:Connect(function(mousepos)
	local bulletSpeed = 500
	local GBullet = bullet:Clone()

	GBullet.Parent = game.Workspace
	print("Bullet is in workspace")

	-- Calculate the bullet's initial position based on the ExitPoint and direction
	GBullet.CFrame = CFrame.new(handle, Vector3.new(mousepos))

	local bulletDir = CFrame.new(Vector3.new(mousepos - ExitPoint))
	GBullet.Velocity = bulletDir * bulletSpeed
	print("Gun Fired")
	Shoot:FireClient(mousepos)
end)

the error means that you are trying to subract a vector3 value to an instance

maybe do

local bulletDir = CFrame.new(Vector3.new(mousepos.Magnitude- ExitPoint))
GBullet.Velocity = bulletDir * bulletSpeed
print("Gun Fired")
Shoot:FireClient(mousepos)

i am now getting this error "Magnitude is not a valid member of Player “Players.Noulted”

You forgot to put the position of the handle

This one is wrong “mousepos” here is the player not the mouse’s position you sent from client it have to be:

Shoot.OnServerEvent:Connect(function(player, mousepos)

You could also do something like this:

local cf = CFrame.new(pos1, pos2)
GBullet.Velocity = cf.LookVector*speed