How to raycast above terrain?

i want to be able to place the part on the terrain but right now it goes straight through


local function CreateRaycast()
    local mousePos = UserInputService:GetMouseLocation()
    local rayCast = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

    local raycastParams = RaycastParams.new()
    raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
    raycastParams.FilterDescendantsInstances = {newPlot, script.Parent}

    local raycastResult = workspace:Raycast(rayCast.Origin, rayCast.Direction * 200, raycastParams)
    return raycastResult
end

Add to the whitelist the Instance Terrain, that just might work.

do i need two different raycastparams to exclude and include?

They way you’re doing it right now is with a whitelist (it’s called includeList now) that only detects newPlot and script.Parent. Everything else (including terrain) doesn’t get hit. Add Terrain to the FilterDescendantsInstances list to include it

i tried to include terrain but it still goes straight through

Can you show the code?


local Farming = {}

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local allPlots = ReplicatedStorage.Systems.Farming:WaitForChild("Plots")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local item = nil
local newPlot = nil
local raycastConnection = nil

local plotCG = "Plots"

local function CreateRaycast(exclude)
	local mousePos = UserInputService:GetMouseLocation()
	local rayCast = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Include
	raycastParams.FilterDescendantsInstances = exclude

	local raycastResult = workspace:Raycast(rayCast.Origin, rayCast.Direction * 200, raycastParams)
	return raycastResult
end

local function placeHolderPlot(name)
	local plotexist = allPlots:FindFirstChild(name)
	if plotexist then
		newPlot = plotexist:Clone()
		newPlot.Parent = workspace.Plots
		
		for i, object in pairs(newPlot:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = plotCG
				object.Material = Enum.Material.ForceField
				object.Transparency = 0.65
				object.Color = Color3.new(0, 1, 0)
			end
		end
	end
end

function Farming.Equipped()
	placeHolderPlot("plotModel")

	raycastConnection = RunService.RenderStepped:Connect(function()
		local tool = script.Parent
		local character = game.Players.LocalPlayer.Character
		local result = CreateRaycast({newPlot, tool, character, workspace.Terrain})

		if newPlot then
			if result and result.Instance then
				local x = result.Instance.Position.X
				local y = result.Instance.Position.Y
				local z = result.Instance.Position.Z

				local cframe = CFrame.new(x, y, z)
				print(cframe)
				newPlot:SetPrimaryPartCFrame(cframe)
			end
		end
	end)
end

function Farming.Unequipped()
	if raycastConnection then
		raycastConnection:Disconnect()
	end
end



return Farming```

Could you try to add a few studs to the raycast mouse position Y?

i tried that too but it doesnt seem to work

That’s actually really weird, I just tested it in my studio and worked fine for me. Are the other parts returning fine and it’s just the terrain or is nothing returning at all?

i just looked and its not even setting the position of the plot at all

The Farming.Equipped() code is the issue?

yeah it was this line

				local x = result.Position.X
				local y = result.Position.Y 
				local z = result.Position.Z