CFrame.angles sometimes rotates more then 45 degrees or none

i almost have finished my placement system, and i am now working on rotating, but sometimes it rotates more then 45 degrees or sometimes doesn’t rotate, here is the code and some pictures:

code:

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local isPlacing = false
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")
local tower = game.ReplicatedStorage.Tower.GroundTower
local CurrentDecoy = nil

function decoy()
	local ray = RaycastParams.new()
	ray.FilterType = Enum.RaycastFilterType.Blacklist
	ray.FilterDescendantsInstances = {player.Character, game.Workspace.Enemies:GetChildren()}
	local unitRay = mouse.UnitRay
	local rayres = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, ray)

	if rayres then
		local Tower = tower:Clone()
		Tower.Parent = workspace
		Tower.Position = rayres.Position + Vector3.new(0, Tower.Size.Y/2, 0)
		Tower.Transparency = .5
		Tower.CanCollide = false
		return Tower
	end
	return nil
end

script.Parent.Activated:Connect(function()
	isPlacing = true
	CurrentDecoy = decoy()
end)

mouse.Move:Connect(function()
	if isPlacing == false  and CurrentDecoy == nil then return end
	local ray = RaycastParams.new()
	ray.FilterType = Enum.RaycastFilterType.Blacklist
	ray.FilterDescendantsInstances = {player.Character, CurrentDecoy, game.Workspace.Enemies}
	local unitRay = mouse.UnitRay
	local rayres = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, ray)

	if rayres then
		CurrentDecoy.Position = rayres.Position + Vector3.new(0, CurrentDecoy.Size.Y/2, 0)
	end
	
	uis.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			
			pcall(function()
				CurrentDecoy.Position = rayres.Position + Vector3.new(0, CurrentDecoy.Size.Y/2, 0)
				CurrentDecoy.Transparency = 0
				CurrentDecoy.CanCollide = true
				CurrentDecoy = nil
			end)
			
			isPlacing = false
		elseif input.KeyCode == Enum.KeyCode.Q then
			pcall(function()
				CurrentDecoy:Destroy()
				CurrentDecoy = nil
				isPlacing = false
			end)
		elseif input.KeyCode == Enum.KeyCode.R then
			CurrentDecoy.CFrame = CurrentDecoy.CFrame * CFrame.Angles(0, math.pi/4, 0) -- HERE
		end
		
	end)
end)

pics:

in this picture you can see it rotated 90 degrees instead of 45.

It looks like you add a new listener to InputBegan every time you move the mouse. When you press ‘R’, it rotates multiple times as a result. To fix this, create the connection only once when the player initiates placement mode, and disconnect it when they cancel placement mode.

1 Like

Or better yet use ContextActionService | Roblox Creator Documentation to bind and unbind the action :slight_smile:

i fixed the code, i just set a deb/cooldown and it works!
thanks for commenting anyways.