Your Angle is just a local variable, so changing it won't have any effect on the actual parent Part. You could use your Angle to modify script.Parent.CFrame directly as a function of time, but this doesn't really account for continuing interactions with other parts, and can even result in overlapping parts. To do this in a more physics-like way, the two options are BodyAngularVelocity, which will have the object attempt to move at a certain rate of angle per time, or BodyGyro, which will apply a given torque to the object to attempt to move it toward a certain orientation.
So here's one possible try, using a BodyAngularVelocity. It assumes you've put a BodyAngularVelocity as a child of the Part using Roblox Studio, leaving the default Name to match the ClassName. Not tested!
local part = Script.Parent
local StartAngle = part.Orientation.Y
local triggerAngle = math.rad(15)
local angVelocity = part:WaitForChild("BodyAngularVelocity")
local angSpeed = math.rad(10)
local maxTorque = 100
local angSign = 0
angVelocity.AngularVelocity = Vector3.new()
angVelocity.MaxTorque = 0
local function AngleDiff(angle1, angle2)
local diff = math.modf(angle1 - angle2, 2*math.pi)
if diff <= math.pi then
diff = diff + 2*math.pi
elseif diff > math.pi then
diff = diff - 2*math.pi
end
end
local function update()
local diff = AngleDiff(part.Orientation.Y - StartAngle)
if angSign == 0 then
if math.abs(diff) >= triggerAngle then
angSign = math.sign(diff)
angVelocity.AngularVelocity =
-angSign * angSpeed * Vector3.FromAxis(Enum.Axis.Y)
angVelocity.MaxTorque = maxTorque
end
elseif math.sign(diff) ~= angSign
angVelocity.AngularVelocity = Vector3.new()
angVelocity.MaxTorque = 0
angSign = 0
end
end
local RunService = game:GetService("RunService")
RunService.Heartbeat:Connect(update)
1 comment
I tried this and first, it came up with the error on line 24: expected vector3, got number. So I then changed it to Vector3.new(0), and it was fixed. Second, it came up with the error on line 28:attempt to perform arithmetic on local 'angle2' (a nil value), and thats where I'm at now