Alarm system with TweenService

Hello there. What I am trying to do is make an alarm rotate about it’s X-axis using TweenService. My first problem was that if I used TweenService to change orientation, it would just simply use the quickest way possible to get to it’s target (to go from 0 to 359, instead of going through all the numbers in between, it just went from 0 straight to 360 and then to 359). To solve that problem, I simply made a number value that starts at 0 and then uses TweenService to move up to 360.

My problem is trying to rotate the bulb on it’s local X-axis. Of what I know, the only way to rotate a part on it’s local axis is to use CFrame.Angles. What I did is that I made a script that waited for the number value that is being Tweened to change, and once it did, it multiplied the bulb’s CFrame by CFrame.Angles(Value of the number value, 0, 0). That line of code was: Bulb.CFrame = Bulb.CFrame * CFrame.Angles(Rotation.Value, 0, 0). That did rotate the bulb on it’s X-axis, but not quite how I expected it to due to the fact that it multiplies what it’s rotation is by how much the targeted rotation is, resulting in this:

https://gyazo.com/e7f8b95ddf590b6f04ecab1ae584878c

The bulb is the cylinder inside the alarm (selected in the image). Here is the alarm in close range:

image

If possible, how could I make the bulb rotate on it’s X-axis using TweenService. If it is not possible doing this with TweenService, how would I make a smooth rotation effect without using TweenService (and some way to control the time it takes to do a full rotation). Thank you for your time and effort.

1 Like

Instead of using a rotation value, you could just rotate by a constant amount per frame using RunService, like so:

local RunService = game:GetService("RunService")

local bulb = -- lightbulb

local rotationSpeed = 720 -- in degrees per second
-- this would be 2 revolutions/second
RunService.RenderStepped:Connect(function(dt)
    bulb.CFrame = bulb.CFrame * CFrame.Angles(math.rad(rotationSpeed) * dt, 0, 0)
end)

Edit: If you truly want to use TweenService, you can separate your singular tween into 3 separate tweens, and just turn by 120 degrees on each tween.

local TweenService = game:GetService("TweenService")

local bulb = -- lightbulb
local info = TweenInfo.new() -- tween info here
-- divide the time by 3!

local Tweens = {}
for i = 1, 3 do
    Tweens[i] = TweenService:Create(
        bulb,
        info,

        -- using radians here
        {CFrame = bulb.CFrame * CFrame.Angles(i * 2*math.pi/3, 0, 0)}
    )
end

while true do
    for _, tween in ipairs(Tweens) do
        tween:Play()
        tween.Completed:Wait()
    end
end
3 Likes

EDIT: Nevermind, I got it. I just needed to turn the bulb light on and change RenderStepped to Heartbeat. Thank you very much.

2 Likes