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)
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.