-
What do you want to achieve?
Set the rotation direction of Tween. When a button is pressed, gears rotate. Green rotates 90, blue rotates -90, and red rotates -180. -
What is the issue?
When rotating by -180, it only rotates one direction, ridding of the “gear” effect.
Here is an image illustrating my goal and comparing it to what happens.
-
What solutions have you tried so far?
I could try making two tweens that would run consecutively, but with my current script it would be inconvenient.
I read up some information on tweens and understand that Tweens move the shortest distance. However, since my parts are moving in the 180 direction, both 180 and -180 should theoretically be the same distance.
The current code is contained in a ModuleScript. When a button is pressed, it activates the module script which inputs information about the button. Each gear has a Configuration object which says what direction the gear should move. Only gears contained in the same room as the button, which is indicated by a tag.
Here is my code:
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local DebrisService = game:GetService("Debris")
local GearScript = {}
GearScript.__index = GearScript
function GearScript.new(Button)
local RotationAmount
if Button.Configuration.ButtonColor.Value == "Green" then
RotationAmount = 90
elseif Button.Configuration.ButtonColor.Value == "Blue" then
RotationAmount = -90
elseif Button.Configuration.ButtonColor.Value == "Red" then
RotationAmount = -180
end
local RoomNumber = Button.Configuration.RoomNumber.Value
RotateAllGears(RoomNumber, RotationAmount)
wait(5)
end
function Rotate(Center, Amount, RotationAxis)
-- RotationDirection is 1 or -1
local RotationDirection = Center.Configuration.RotationDirection.Value
local info = TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local goals = {
CFrame = Center.CFrame * CFrame.fromAxisAngle(RotationAxis, math.rad(Amount)*RotationDirection)
}
local tween = TweenService:Create(Center, info, goals)
tween.Parent = Center
tween:Play()
--Add the tween to DebrisService
DebrisService:AddItem(tween,2)
end
function RotateAllGears(RoomNumber, RotationAmount)
--Goal: Rotate all gears within a room.
--Find all gears in the world, and see which are in the same RoomNumber as the button.
for _,part in pairs(CollectionService:GetTagged("Gears")) do
if part.Configuration.RoomNumber.Value == RoomNumber then
if not part:FindFirstChild("Tween") then
if part.Configuration.GearType.Value == "Horizontal" then
Rotate(part, RotationAmount, Vector3.new(0,1,0))
elseif part.Configuration.GearType.Value == "Vertical" then
Rotate(part, RotationAmount, Vector3.new(1,0,0))
end
end
end
end
end
return GearScript
Your help is very much appreciated. Thank you.