Place System rotating the wrong way

I have a place system that just replicates and clones the decided object from replicated storage and puts it where the clients mouse is but its rotating sideways for some reason.

image
image

Remote Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage.Remotes:WaitForChild("Place")
local Structures = ReplicatedStorage:WaitForChild("Structures")

PlaceStructure.OnServerInvoke = function(player, StructureName, StructureCFrame)
	local crafted
	local RealStructure = Structures:FindFirstChild(StructureName):Clone()
	
	if RealStructure then
		RealStructure.CFrame = StructureCFrame
		RealStructure.Parent = game.Workspace
		RealStructure.Owner.Value = player.Name
		RealStructure.ObjectName.Value = RealStructure.Name
		crafted = true
	else
		crafted = false
		
	end
	return crafted
	
end

Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage.Remotes:WaitForChild("Place")
local Structures = ReplicatedStorage:WaitForChild("Structures")

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

local player = game:GetService("Players").LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 2
local maxPlacingDistance = 50
local zKeyIsPressed = false
local placingStructure = false
local yOrientation=0
local goodTOPlace = false
local placedStructure
local clientStructure

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

	local rotationSpeed = 5
	while zKeyIsPressed 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.Z then
		zKeyIsPressed = 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()
				StructureFrame.Visible = true
			end
		end
	end
end

-- Handles general input (mouse or keyboard) input
function handleInputStarted(input)
	if input.KeyCode == Enum.KeyCode.Z 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 = {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")
	else
		goodTOPlace = false
		clientStructure.BrickColor = BrickColor.new("Crimson")
	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)
	StructureFrame.Visible = false

	if placingStructure == false then
		placingStructure = true

		clientStructure = Structures:FindFirstChild(structureName):Clone()
		clientStructure.BrickColor = BrickColor.new("Forest green")
		clientStructure.Material = "Neon"
		clientStructure.CanCollide = false
		clientStructure.Parent = game.Workspace

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

		RunService.RenderStepped:Connect(handleRenderStepped)
	end
end

-- Iterate over all Buttons in the StructureFrame and attach a mouse listener
for _, structureButton in pairs(StructureFrame:GetChildren()) do
	if structureButton:IsA("TextButton") then
		structureButton.MouseButton1Up:Connect(function() 
			handleBrickButtonPressed(structureButton.Name)	
		end)
	end
end


UIS.InputBegan:Connect(handleInputStarted)
UIS.InputEnded:Connect(handleKeyInputEnded)
---

I have yet to find a solution to it still, I don’t see anything that has to do with the objects rotation besides the pressed z key function which is separate to the error.

I am not sure if i am correct but i think You should change this part of your code
local startingCFrame = CFrame.new(0, 0, 0)
because i am not sure if this is z or x direction that it is rotated at i will just tell you write to you all the possible solutions(it might not be any of those solutions). Just replace this local startingCFrame = CFrame.new(0, 0, 0) with the down solutions.

1st solution: local startingCFrame = CFrame.new(0, 0, 0)* CFrame.Angles(math.rad(-90), 0, 0)
2st solution local startingCFrame = CFrame.new(0, 0, 0)* CFrame.Angles(0, 0, math.rad(-90))
3st solution: local startingCFrame = CFrame.new(0, 0, 0)* CFrame.Angles(math.rad(90), 0, 0)
4st solution local startingCFrame = CFrame.new(0, 0, 0)* CFrame.Angles(0, 0, math.rad(90))

Edit: 2 new possible solutions
local startingCFrame = CFrame.new(0, 0, 0)* CFrame.Angles(0, math.rad(90), 0)
local startingCFrame = CFrame.new(0, 0, 0)* CFrame.Angles(0, math.rad(-90), 0)

Or you could rotate it manually

1 Like

None of those solutions worked for me sadly:
image

1 Like

I’ve red the code again and i saw that there is another part where you set the cframe of the part
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.z)
clientStructure.CFrame = newCFrame * newAnglesCFrame
maybe try chaging newAnglesCFrame. You know as you did to the other ones

Yes you found where the error is!. So I set the local newAnglesCFrame = CFrame.Angles(20,20,15) to that and it rotated the object a bit. Do you know the exact number values to set it straight.

Heres what (20,20,15) set the objects rotation to:
image

1 Like

first of all you are using rads instead of degrees. Because i dont in what axis its rotated at you should try these

local newAnglesCFrame = CFrame.Angles(math.rad(-90), math.rad(yOrientation), 0)
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), math.rad(-90))
local newAnglesCFrame = CFrame.Angles(math.rad(90), math.rad(yOrientation), 0)
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), math.rad(90))
Its one of them

1 Like

Thanks you fixed my problem. image

1 Like