Raycast is buggy

  1. What do you want to achieve?
    For the object to move smoothly instead of buggy.

  2. What is the issue?
    It doesn’t move smoothly at all.

  3. What solutions have you tried so far?
    I added the blacklist option but it didn’t work.

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local ReplicatedStorageService = game:GetService("ReplicatedStorage")

local Towers = ReplicatedStorageService:WaitForChild("Towers")

local TowerToSpawn 
local Gui = script.Parent

local Camera = workspace.CurrentCamera

function AddPlaceholderTower(name)
	local Tower = Towers:FindFirstChild(name)
	if not Tower then
		print(name, "doesn't exist!")
		return
	end
	
	TowerToSpawn = Tower:Clone()
	TowerToSpawn.Parent = workspace.Towers
end

function MouseRaycast(blacklist)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	local MousePosition = UserInputService:GetMouseLocation()
	local MouseRay = Camera:ViewportPointToRay(MousePosition.X, MousePosition.Y)
	local RaycastResult = workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 1000)
	
	return RaycastResult
end

UserInputService.InputBegan:Connect(function(Input, processed)
	if processed then return end
	
end)

RunService.RenderStepped:Connect(function()
	if TowerToSpawn then
		local Result = MouseRaycast({TowerToSpawn})
		if Result and Result.Instance then
			local X = Result.Position.X
			local Y
			local Z = Result.Position.Z

			if TowerToSpawn.Humanoid.RigType == Enum.RigType.R15 then
				Y = Result.Position.Y + TowerToSpawn.Humanoid.HipHeight + (TowerToSpawn.PrimaryPart.Size.Y / 2)
			else
				Y = TowerToSpawn:GetExtentsSize().Y / 2
			end

			local cframe = CFrame.new(X,Y,Z)
			TowerToSpawn:SetPrimaryPartCFrame(cframe)

		end
	end
end)

Gui.Activated:Connect(function()
	AddPlaceholderTower(Gui.TowerName.Value)
end)

You should check to see if any of the inputs to Y are changing as you move the mouse. I don’t know why they would but it’s all I can think of.

The issue is that the object is not moving smoothly due to it being updated in the RenderStepped function. You can fix this issue by using the MoveTo function to smoothly move the object instead of manually setting its position.