Trying to use UIS mouse for placement system not working

Im trying to make a placement system using UIS:GetMouseLocation() but when i try to place a block i get lots of errors, any help?

RunService.RenderStepped:Connect(function()
				local mouse = UIS:GetMouseLocation()
				local mouseRay = camera:ScreenPointToRay(mouse.Y, mouse.X)
				local raycastParams = RaycastParams.new()
				raycastParams.FilterDescendantsInstances = {clientStructure, char}
				raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
				local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 20, raycastParams)

				if rayResult then
					goodtoplace = true
					clientStructure.BrickColor = BrickColor.new("Forest green")
					print(mouseRay.Direction)
				else
					clientStructure.BrickColor = BrickColor.new("Crimson")
				end
				
				local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
				local newCFrame = CFrame.new(rayResult.Position.X, rayResult.Position.Y, rayResult.Position.Z)
				clientStructure.CFrame = newCFrame
			end)
		end

Full Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Blocks = ReplicatedStorage:WaitForChild("Blocks")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local camera = game.Workspace.CurrentCamera


local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local yBuildingOffset = 5
local maxPlacingDistance = 50
local rPressed = false
local placingStructure = false

for _, structureButton in pairs(StructureFrame:GetChildren()) do
	structureButton.MouseButton1Up:Connect(function()
		local yOrientation = 0
		local goodtoplace = false
		local placedstructure

		if placingStructure == false then
			placingStructure = true

			local clientStructure = Blocks:FindFirstChild(structureButton.Name):Clone()
			clientStructure.BrickColor = BrickColor.new("Forest green")
			clientStructure.Material = "Neon"
			clientStructure.CanCollide = false
			clientStructure.Parent  = workspace

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

			RunService.RenderStepped:Connect(function()
				local mouse = UIS:GetMouseLocation()
				local mouseRay = camera:ScreenPointToRay(mouse.Y, mouse.X)
				local raycastParams = RaycastParams.new()
				raycastParams.FilterDescendantsInstances = {clientStructure, char}
				raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
				local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 20, raycastParams)

				if rayResult then
					goodtoplace = true
					clientStructure.BrickColor = BrickColor.new("Forest green")
					print(mouseRay.Direction)
				else
					clientStructure.BrickColor = BrickColor.new("Crimson")
				end
				
				local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
				local newCFrame = CFrame.new(rayResult.Position.X, rayResult.Position.Y, rayResult.Position.Z)
				clientStructure.CFrame = newCFrame
			end)
		end

	end)
end

Its swapped it should be x them Y like X,Y. This has caused the offset error.

https://developer.roblox.com/en-us/api-reference/function/Camera/ScreenPointToRay

That length is pretty short, default for Mouse object is 1000 studs.

You can shorten it please.

CFrame.new(rayResult.Position)

Also this needs to be under the if raycast result to avoid the nil error when ray doesn’t hit anything.

If it doesn’t hit anything then do raycast origin plus raycast direction to get the end position of the ray.