Model not following mouse

Hello,

I am making a tower defense game. I am trying to make the tower but I need some help. The tower is not following the mouse like it supposed to. Idk why it happens. I have a test button and after I press it. This happened:
image
Here are the script:

local function raycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams= RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local result = workspace:Raycast(mouseRay.Origin,mouseRay.Direction * 100,raycastParams)
end

local function removePlaceholder()
	if placeholder then
		placeholder:Destroy()
		placeholder = nil
	end
end

local function addPlaceholder(name)
	-- remove previous placeholder
	removePlaceholder()
	-- add placeholder
	local towerExists = ReplicatedStorage.Tower:FindFirstChild(name)
	if towerExists then
		placeholder = towerExists:Clone()
		placeholder.Parent = camera
	end
end

gameGui.Test.Activated:Connect(function()
	addPlaceholder(gameGui.Test.Text)
end)

RunService.RenderStepped:Connect(function()
	-- control mouse
	if placeholder then
		-- raycasting
		local result = raycast({placeholder})
		if result and result.Instance then
			local x = result.Position.X
			local y = result.Position.Y -- the height offset
			local z = result.Position.Z
			local cframe = CFrame.new(x,y,z)
			placeholder:SetPrimaryPartCFrame(cframe)
		end
	end
end)

please tell me what I did wrong. Ty

1 Like

Try reading
https://developer.roblox.com/en-us/api-reference/property/Mouse/Hit

i tried using it but still giving me the same result. any other way to fix this?

Could you please send the code and provide footage?

nvm, I fixed it. Just by doing a little change to the raycast function. I forgot to return the result variable lol. btw thx for trying to help. Have a nice day sir