Raycast CFrame not accurate at random intervals

I have a Raycast code that makes it so you can place down Towers in my Tower Defense game, and at rare intervals, the raycast CFrame is a location that the mouse was NOT at. For example, in the middle of the map (not the origin weirdly enough). Is there any way to make this Raycast more accurate? I used this video as a Tutorial. Tower Placement - Tower Defense Tutorial #6 - YouTube

Local Script with Raycast in it:

local function MouseRaycast(model)
	local mousePosition = UserInputService: GetMouseLocation()
	local mouseRay = camera: ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()
	local blacklist = camera:GetChildren()
	for i, playerpart in pairs(Players:GetPlayers()) do
		local playerpartstoadd = player.Character:GetChildren()
		table.insert(blacklist, playerpartstoadd)
	end
	table.insert(blacklist, model)
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	
	return raycastResult
end

RunService.RenderStepped:Connect(function()
	local result = MouseRaycast(towerToSpawn)
	if result and result.Instance then
		if towerToSpawn then
			hoveredInstance = nil
			currentbearheld = towerToSpawn.Name
			if result.Instance.Parent.Name == "TowerArea" or result.Instance.Parent.Parent.Name == "TowerArea" then
				canPlace = true
				ColorPlaceholderTower(Color3.fromRGB(139, 255, 133))
			else
				canPlace = false
				ColorPlaceholderTower(Color3.fromRGB(255, 121, 123))
			end
			local x = result.Position.X
			local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 2)
			local z = result.Position.Z

			local cframe = CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation), 0)
			towerToSpawn:SetPrimaryPartCFrame(cframe)
		else
			hoveredInstance = result.Instance
		end
	else
		hoveredInstance = nil
	end
	
end)


Part of local script that invokes server to spawn tower:

local function SpawnNewTower()
		if canPlace then
			local placedtower = spawnTowerFunction:InvokeServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
			if placedtower then
				placedTowers += 1
				gui.Info.BearLimitFrame.BearLimit.Text = "Bears: " .. placedTowers .. "/" .. maxTowers
				holdingbear = false
				RemoveBear()
				UpdateToggleView()
				currentbearheld = nil
			end
		end
end

Not sure what i need to send, most of the code is from the tutorial, just wanted to post what i thought would be important for figuring out this issue.

Try using mouse.Hit instead of casting you rays from the mouses position on the screen

This is what happens when i change the placement itself to Mouse.Hit. It takes the currentcamera and just makes the tower “point” at where i hit the mouse button.

What exactly does the original offset look like? Also try using the camera’s CFrame’s position instead of the rays origin

Also what if you try blacklisting the player’s character instead

I found the problem it’s here

local raycastResult = workspace:Raycast(mouseRay.Origin, -> <- mouseRay.Direction  * 1000, raycastParams)

its ray casting to the direction but not from the origins postion try adding the mouseRay.Origin to the mouseRay.Direction

mouseRay.Origin + mouseRay.Direction * 1000