Placing System Errors

I am creating to simple placing system but I ran into an error. This error is that the part’s position always is hovering above the ground. I can’t seem a way to fix this and it might because of a weird property with the terrain.

I would love to know what you think! :slight_smile:

Here are the scripts:

Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

--local player = game.Player.LocalPlayer
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 2
local maxPlacingDistance = 5
local rKeyIsPressed = false
local placingStructure = false
local yOrientation=0
local goodTOPlace = false
local placedStructure
local clientStructure

-- Handles start of temporary brick rotation
function handleKeyInputStarted(input)
	rKeyIsPressed = true

	local rotationSpeed = 5
	while rKeyIsPressed do
		wait()
		if placingStructure == true then
			yOrientation = yOrientation + rotationSpeed								
		end
	end

end

-- Handles end of temporary brick rotation
function handleKeyInputEnded(input)
	if input.KeyCode == Enum.KeyCode.R then
		rKeyIsPressed = false						
	end
end

-- Handles construction of actual Brick when user is satisfied with placement/rotation 
function handleMouseInputBegan(input)
	if placingStructure ==  true then
		if goodTOPlace == true then
			local StructureCFrame = clientStructure.CFrame
			placedStructure = PlaceStructure:InvokeServer(clientStructure.Name, StructureCFrame)

			if placedStructure == true then
				placingStructure = false
				clientStructure:Destroy()
			end
		end
	end
end

-- Handles general input (mouse or keyboard) input
function handleInputStarted(input)
	if input.KeyCode == Enum.KeyCode.R then
		handleKeyInputStarted(input)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		handleMouseInputBegan(input)
	elseif input.KeyCode == Enum.KeyCode.X then
		return
	end
end

-- Handles rendering the temporary Brick during placement
function handleRenderStepped()
	local mouseRay = mouse.UnitRay
	local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
	local ignoreList = {clientStructure, char}
	local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

	if hit and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDistance then
		goodTOPlace = true
		clientStructure.BrickColor = BrickColor.new("Forest green")

		clientStructure.Transparency = 0.5
	else
		goodTOPlace = false
		clientStructure.BrickColor = BrickColor.new("Crimson")
		clientStructure.Transparency = 0.5
	end

	local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
	local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.z)
	clientStructure.CFrame = newCFrame * newAnglesCFrame
end

-- Handles constructing the temporary placement brick when the create button is pressed.
function handleBrickButtonPressed(structureName)

	if placingStructure == false then
		placingStructure = true

		clientStructure = Structures:FindFirstChild(structureName):Clone()
		clientStructure.Material = "Neon"
		clientStructure.BrickColor = BrickColor.new("Forest green")

		clientStructure.CanCollide = false
		clientStructure.Parent = game.Workspace

		local startingCFrame = CFrame.new(0, -2, -15)
		clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)

		RunService.RenderStepped:Connect(handleRenderStepped)
	end
end

-- Iterate over all Buttons in the StructureFrame and attach a mouse listener
MyMouse = game.Players.localPlayer:GetMouse()
Player = game.Players.localPlayer

MyMouse.KeyDown:connect(function(key)
	if (key:byte() == 103) then
		handleBrickButtonPressed("Torch")	

	end
end)




UIS.InputBegan:Connect(handleInputStarted)
UIS.InputEnded:Connect(handleKeyInputEnded)
Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")

PlaceStructure.OnServerInvoke = function(player, StructureName, StructureCFrame)
	
	local Crafted
	local realStructure = Structures:WaitForChild(StructureName):Clone()
	
	if realStructure then
		realStructure.CFrame = StructureCFrame
		realStructure.Parent = game.Workspace
		Crafted = true
	else
		Crafted = false
	end
	
	return Crafted
	
end

Video Of Error