I have used some code I found on the devforum (Making a rotate tool - #8 by AdvancedDrone) to create a rotation tool. However, despite using essentially the exact same code (only modified for models instead of parts), I have a bizarre issue where rotating the object even a little causes it to spin rapidly and freak out. I am not sure what is causing the issue.
And the code itself (all based off of the devforum post above…)
local Player = game.Players.LocalPlayer
local originCF = CFrame.identity
local increment = 5
local function Round(number)
--local increment = Player.Character:WaitForChild("PlayerData").RotationSize.Value
return math.floor((number / increment) + 0.5) * increment
end
local function AngleFromAxis(axis, r)
local relativeAngle = math.rad(Round(math.deg(r)))
return axis == Enum.Axis.X and {relativeAngle, 0, 0}
or axis == Enum.Axis.Y and {0, relativeAngle, 0}
or axis == Enum.Axis.Z and {0, 0, relativeAngle}
end
script.Parent.MouseDrag:Connect(function(axis, relativeAngle, delta)
script.Parent.Adornee.Parent:PivotTo(script.Parent.Adornee.Parent.PrimaryPart.CFrame * CFrame.Angles(unpack(AngleFromAxis(axis, relativeAngle))))
end)
script.Parent.MouseButton1Down:Connect(function()
originCF = script.Parent.Adornee.Parent.PrimaryPart.CFrame
end)
script.Parent.MouseButton1Up:Connect(function()
script.Parent.RemoteEvent:FireServer(script.Parent.Adornee.Parent, script.Parent.Adornee.Parent.PrimaryPart.CFrame)
end)
Any help would be appreciated. The code works in the video provided in the post I linked, but I can’t get it to work in the same way.
Too fast or too slow of a rotation is usually caused by incorrectly converting between degrees and radians. I would double-check the code to make sure it’s not accidentally running math.deg() on an angle that’s already measured in degrees!
So, experimenting a little, I changed the code to have print functions for r and relativeAngle within the AngleFromAxis function.
Initial results just show that r changes very slightly from something like 0.351657.... to 0.349065....
Removing the math.deg() from the function and just using math.rad(Round(r)), it changes that initial number to zero nearly every time except for when I seem to rotate it clockwise, and then it registers something like 0.8 but doesn’t actually rotate the model any further than that and just sticks there and starts registering 0’s again. All of that are pretty minute and weird details that I don’t think matter to the bigger picture, to be honest.
Anyways, I would assume keeping math.deg() in the function will probably be the better course of action for now, perhaps, since it at least gives me something that isn’t 0?
Still experiencing issues with this. I know bumping is frowned upon, but I’d like some assistance! I don’t actually know where to go from here in terms of fixing this code to work and I have yet to find solutions on the devforum after more searching.
So, the solution I have marked up is because of an issue with how ROBLOX returns the data for the relativeAngle. This was a very annoying issue to resolve because of how little documentation there is on ArcHandles… I would genuinely recommend updating the documentation to be a bit more informative.
Basically, you should just change the relativeAngle from the MouseDrag event to degrees, and then round it, and then put it back into radians.
End result code looks like this, after a lot of changes and iterations:
local Player = game.Players.LocalPlayer
local originCF = CFrame.identity
function SnapToClosestIncrement(num)
local increment = Player.Character:WaitForChild("PlayerData").RotationSize.Value
local rotateDegrees = math.deg(num)
local snapNum = math.floor(rotateDegrees / increment + 0.5) * increment
return math.rad(snapNum)
end
script.Parent.MouseDrag:Connect(function(axis, relativeAngle, delta)
local adjustedAngle = SnapToClosestIncrement(relativeAngle)
if axis == Enum.Axis.X then
script.Parent.Adornee.Parent:PivotTo(originCF * CFrame.Angles(adjustedAngle, 0, 0))
end
if axis == Enum.Axis.Y then
script.Parent.Adornee.Parent:PivotTo(originCF * CFrame.Angles(0, adjustedAngle, 0))
end
if axis == Enum.Axis.Z then
script.Parent.Adornee.Parent:PivotTo(originCF * CFrame.Angles(0, 0, adjustedAngle))
end
end)
script.Parent.MouseButton1Down:Connect(function()
originCF = script.Parent.Adornee.Parent.PrimaryPart.CFrame
end)
script.Parent.MouseButton1Up:Connect(function()
script.Parent.RemoteEvent:FireServer(script.Parent.Adornee.Parent, script.Parent.Adornee.Parent.PrimaryPart.CFrame)
end)