Help with placement system!

I have a script I made and have been studying to get better at scripting. I edited it so when I press r the green part(Image below) rotates to the right, but when I press Q I want the part to rotate to the left.

What I wanna achieve with this is so the script will make it turn right/left 90 degrees instead of little increments at a time.(If possible a grid system too!) And also again, the Q to rotate the part left is not working. I have tried editing the script multiple times that past couple hours and I can’t get it to work. Here is the script!(Sorry for it being long)

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.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 = 5

local maxPlacingDistance = 50

local rKeyIsPressed = false

local qKeyIsPressed = false

local placingStructure = false


local names = {"WoodWall", "StoneWall", "Place"}

for _, structureButton in pairs(StructureFrame:GetChildren()) do

 if structureButton:IsA("TextButton") then

  structureButton.MouseButton1Up:Connect(function()

   

   StructureFrame.Visible = false

   

   local yOrientation = 0

   local goodToPlace = false

   local placedStructure

   

   if placingStructure == false then

    placingStructure = true

   

    local clientStructure = Structures:FindFirstChild(structureButton.Name):Clone()

    clientStructure.BrickColor = BrickColor.new("Forest green")

    clientStructure.Material = "Neon"

    clientStructure.CanCollide = false
	
	clientStructure.Transparency = 0.5
	
	clientStructure.Anchored = true

    clientStructure.Parent = game.Workspace

    

    local startingCFrame = CFrame.new(0,-2,-15)

    clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)

    

    RunService.RenderStepped:Connect(function()

     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 hit.Name == names or "Place" 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)

	UIS.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.R then
			rKeyIsPressed = false
		end
		
		if input.KeyCode == Enum.KeyCode.Q then
			qKeyIsPressed = false
		end
	end)

    UIS.InputBegan:Connect(function(input)

     if input.KeyCode == Enum.KeyCode.R then

      rKeyIsPressed = true
	
	if input.KeyCode == Enum.KeyCode.Q then
	  qKeyIsPressed = true
	end

      

      local rotationSpeed = 5

      while rKeyIsPressed do

       wait()

       if placingStructure == true then

        yOrientation = yOrientation + rotationSpeed

  

       end

      end
	
	 while qKeyIsPressed do

       wait()

       if placingStructure == true then

        yOrientation = yOrientation - rotationSpeed

  

       end

      end

     end

    end)

    

    UIS.InputBegan:Connect(function(input)

     if input.UserInputType == Enum.UserInputType.MouseButton1 then

      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

    end)

   end

  end)

 end

end
2 Likes

Oh my gosh, after looking at the script I fixed the Q to left problem :man_facepalming:

But if you have an answer for a grid system or rotating it left/right at 90degrees. Please reply!

1 Like

Off topic;

My Suggestion

From personal experience, replacing the while loop with .RenderStepped instead will drastically improve smoothness.

Just a suggestion :smiley:

2 Likes

Thanks, will do!

(30 charsssss)

1 Like

change this: yOrientation = yOrientation + rotationSpeed
to: yOrientation = yOrientation + 90

2 Likes