Part resized by raycast result returns incorrect sizes

Hi there, I have a simple that script that spawns a part based of mouse your position. What it does, is that it creates a part, sets it CFrame, to the raycast results, along with lookat the part it hit, and then set it’s size. The issue is, is that it can sometimes spawn really small parts along with the z-axis. I don’t know why this is happening, but I have a feeling it could be raycast that’s inaccurate.

What happens:
image

The issue that happens:
image

image

Local script:

local remote = owner.Character.RemoteEvent
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
	remote:FireServer(mouse.Hit.Position)
end)

ServerScript:

local owner = game.Players.vxsqi
local remote = Instance.new("RemoteEvent",owner.Character)
local function raycast(mousePos:Vector3)
	local humanoidrootpart = owner.Character.HumanoidRootPart
	local result = workspace:Raycast(humanoidrootpart.Position, (mousePos - humanoidrootpart.Position).Unit*1000)
	
	if result then
		local hit = result.Position
		local laser = Instance.new("Part",script)
		laser.Anchored = true
		laser.CanCollide = false
		laser.CFrame = CFrame.lookAt(humanoidrootpart.Position:Lerp(hit,.5),hit)
		laser.Size = Vector3.new(.3,.3,(humanoidrootpart.Position-hit).Magnitude)
		print(hit)
	end
end

remote.OnServerEvent:Connect(function(plr,pressed)
	if plr == owner then
		raycast(pressed)
	end
end)

are you ignoring all the current bullets? it could just be because its hitting the current bullet already put there

Nope, still hasn’t fixed it.

Is this what you meant?

local function raycast(mousePos:Vector3)
	local raycastparams = RaycastParams.new()
	local humanoidrootpart = owner.Character.HumanoidRootPart
	
	raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
	local result = workspace:Raycast(humanoidrootpart.Position, (mousePos - humanoidrootpart.Position).Unit*1000,raycastparams)
	
	if result then
		local hit = result.Position
		local laser = Instance.new("Part",script)
		raycastparams.FilterDescendantsInstances = {laser}
		laser.Anchored = true
		laser.CanCollide = false
		laser.CFrame = CFrame.lookAt(humanoidrootpart.Position:Lerp(hit,.5),hit)
		laser.Size = Vector3.new(.3,.3,(humanoidrootpart.Position-hit).Magnitude)
		print(hit)
	end

end

almost, your filterdescendantsinstances is applied after the raycast

local function raycast(mousePos:Vector3)
	local raycastparams = RaycastParams.new()
	local humanoidrootpart = owner.Character.HumanoidRootPart
	
	raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastparams.FilterDescendantsInstances = {script}
	local result = workspace:Raycast(humanoidrootpart.Position, (mousePos - humanoidrootpart.Position).Unit*1000,raycastparams)
	
	if result then
		local hit = result.Position
		local laser = Instance.new("Part",script)--dont use the second parameter it causes more networking
		laser.Anchored = true
		laser.CanCollide = false
		laser.CFrame = CFrame.lookAt(humanoidrootpart.Position:Lerp(hit,.5),hit)
		laser.Size = Vector3.new(.3,.3,(humanoidrootpart.Position-hit).Magnitude)
		laser.Parent = script--instead parent it after everything
		print(hit)
	end

end