Code issues with placement system

I based on DevKing’s tutorial to make a placement system, of course I modified it to take a model and not a part, now I tested it with only the primary part and everything is fine. and not a part, now I tried it with only the primary part and everything is fine, it takes the position well, but when I use a model it happens strange things, the model is approaching or moving away, sometimes it works normal, I tried to use the tutorial of Ego Mouse but apparently he anchors it to the canvas, although I use his idea to make a bounding box. to make a bounding box.

It seems that in the first instant the position is well detected.

Here I leave the script, the problem must be in the handleRenderStepped, but I can’t think of a good way to solve it.

script:

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

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


--local player = game.Player.LocalPlayer
local player = game:GetService("Players").LocalPlayer
local BuildFrame = player.PlayerGui.Menu.BuildingMenu
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset
local maxPlacingDistance = 100
local rKeyIsPressed = false
local placingStructure = false
local yOrientation = 0
local goodTOPlace = false
local placedStructure
local clientStructure
local CF
local AuxZ

-- Silo, Garage, Field, Fence, Barn, ChickenCoop, Hedge

local dexOffset = {17.7, 13, .2, 2.6, 33.78, 14.551, 4}

local ListBuild = BuildFrame.Build.MenuBuild.ScrollingFrame

-- Handles start of temporary brick rotation
function handleKeyInputStarted(input)
	yOrientation = yOrientation + 90
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 = CF.CFrame
			placedStructure = PlaceStructure:InvokeServer(CF.Name, StructureCFrame)

			if placedStructure == true then
				placingStructure = false
				CF:Destroy()
				wait(.3)
				BuildFrame.Enabled = true
			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)
	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 = {CF, char}
	local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

	if hit and (hit:IsA("Terrain")) and (HumanoidRootPart.Position - CF.Position).Magnitude < maxPlacingDistance then
		goodTOPlace = true
		CF.BrickColor = BrickColor.new("Forest green")
	else
		goodTOPlace = false
		CF.BrickColor = BrickColor.new("Crimson")
	end
	
	
	if clientStructure.PrimaryPart then
		local newAnglesCFrame = CFrame.Angles(0,math.rad(yOrientation),math.rad(AuxZ))
		local newCFrame = CFrame.new(position.X, position.Y, position.z)
		local suchFrame = newCFrame * newAnglesCFrame
		clientStructure:SetPrimaryPartCFrame(suchFrame)
		print(tostring(clientStructure.PrimaryPart.Position))
	end
end

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

	if placingStructure == false then
		placingStructure = true

		clientStructure = Structures:FindFirstChild(structureName):Clone()
		CF = clientStructure.PrimaryPart
		CF.BrickColor = BrickColor.new("Forest green")
		CF.Transparency = .9
		CF.Material = "Neon"
		CF.CanCollide = false
		clientStructure.Parent = workspace

		if clientStructure.PrimaryPart then
			local startingCFrame = CFrame.new(0, -2, -15)
			local c = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
			clientStructure:SetPrimaryPartCFrame(c)
		end
		
		RunService.RenderStepped:Connect(handleRenderStepped)
		
	end
end

-- Iterate over all Buttons in the StructureFrame and attach a mouse listener
for index, structureButton in pairs(ListBuild:GetChildren()) do
	if structureButton:IsA("TextButton") then
		structureButton.MouseButton1Up:Connect(function()
			yBuildingOffset = dexOffset[index]
			if yBuildingOffset == dexOffset[1] then
				AuxZ = -90
			else
				AuxZ = 0
			end
			handleBrickButtonPressed(structureButton.Name)	
		end)
	end
end


UIS.InputBegan:Connect(handleInputStarted)
UIS.InputEnded:Connect(handleKeyInputEnded)
1 Like

Firstly, EgoMoose is not a mouse, Secondly you had a problem with the math I think at: