Placing system going haywire

I am making a simple placing system, I am having an issue where the part just goes haywire, here’s a gif.
https://gyazo.com/731e14215d6b1eb8cecf69f9fe6f5898.gif
The part is also not going red when it’s too far away, when it checks the distance at line 14.

	renderstep = game:GetService("RunService").RenderStepped:Connect(function()
		pcall(function()
			local newarea = mouse.Hit.p
			local calculations = (newarea - player.Character.HumanoidRootPart.Position).magnitude
				local params = RaycastParams.new(); params.FilterDescendantsInstances = {workspace.Map.Effects}; params.FilterType = Enum.RaycastFilterType.Blacklist
			local ray = workspace:Raycast(newarea, Vector3.new(0,-10000,0), params)
			if ray and ray.Position then
				fakewall:SetPrimaryPartCFrame(CFrame.new(Vector3.new(newarea.X, ray.Position.Y, newarea.Z)))
				if calculations <= 30 then
					for i,v in pairs (fakewall) do
						v.BrickColor = BrickColor.new("Bright green")
						area = newarea
					end
				elseif calculations >= 31 then
					for i,v in pairs (fakewall) do
						v.BrickColor = BrickColor.new("Really red")
						area = nil
					end

				end
				end
		end)
	end)

I think that may be happening because the fake wall is being detected by your mouse, try doing this:

mouse.TargetFilter = InsertTheFakeWallHere
1 Like

Maybe try using mouse.Origin instead of mouse.Hit.p

Tried it, still doesn’t work properly.

Doesn’t mouse.origin return a CFrame?

Well yeah, but you can prob use mouse.Origin.p

mouse.Origin gives you the CFrame of the current camera, if you want the CFrame of what the mouse is hitting use mouse.Hit

I am using Mouse.Hit.p, it just goes haywire like I showed you in the gif. Mouse.Origin is just as wonky, mouse.origin just covers my screen

Oh alr, I don’t really know any other way than mouse.Hit.p.

Ive had this problem before. Essentially, you are trying to place your object on what ever the mouse hits. And since you don’t have anything in your code that ignores the part being placed, your mouse is hitting that part.

I solved this issue by using Camera:ScreenPointToRay() and RayCasting. In my opinion, this method is best because it is relatively cheap, efficient, and you can do a lot more with this than the mouse instance. Such as having a whitelist or blacklist for parts.

1 Like