Placement Script not working as expected

Hey guys.

So, I’ve tried twice to make a Placement System based on Mouse.Hit. The thing is, every time, it comes closer and closer to the camera before finally stopping and falling to the ground (not what I want).

Here’s my Placement Service module:

local _PS = {}

local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local IsBuilding = true

local PreviewModel

function _PS:CreatePreviewModel(item)
	local model = game.ReplicatedStorage.Buildables[item]:Clone()
	model.Parent = game.Workspace
	for _, part in pairs(model:GetChildren()) do
		if part:IsA("Part") or part:IsA("UnionOperation") or part:IsA("MeshPart") then
			part.BrickColor = BrickColor.new("Bright blue")
			part.Transparency = 0.5
		end
	end
	
	PreviewModel = model
end

function _PS:UpdatePreviewModelCFrame()
	if not IsBuilding then return end
	if PreviewModel == nil then _PS:CreatePreviewModel("Test") end
	print("Updating...")
	PreviewModel:SetPrimaryPartCFrame(mouse.Hit)
end

return _PS

and here’s my LocalScript that manages when to update:

local PlacementService = require(game.ReplicatedStorage.Modules.PlacementService)

local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local IsBuilding = true

mouse.Move:Connect(function()
	if not IsBuilding then return end
	PlacementService:UpdatePreviewModelCFrame()
end)

I’ve tried time and time again, but I just can’t get it to work properly.

Thanks!

EDIT: here’s a video of what’s happening.

it looks like the part is not anchored.

Anchoring makes it happen every time you move your mouse.

change that to this:

PreviewModel:SetPrimaryPartCFrame(mouse.Hit.p)

That throws an error. You need to use

PreviewModel:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))

and it also still has the problem.

Use the raycasting for this so you can ingore the preview model

local unitray = mouse.UnitRay
local range = 100
local raycastray = Ray.new(unitray.Origin,unitray.Direction*100)
local _,pos = workspace:FindPartOnRay(raycastray,PreviewModel)
PreviewModel:SetPrimaryPartCFrame(pos)

Works perfectly. Thanks so much! :slight_smile: