Hello, developers! Back at the DevForum again looking for some answers, and this is my question:
How do you make a spinning alarm light?
The main activation is triggered through an onclick function (I currently have programmed in the announcements and special effects) however I still need the code for making a spinning alarm light.
The primary part that spins is the shield (while the actual neon red bit itself doesnt spin) so that it looks like it’s actually spinning, and it has a PointLight in it.
A lot of the times code for rotating an alarm light doesn’t work for all angles, so if you have a good code, then please let me know!
For the bulb, turn off can collide for two parts, add two attachments and a hinge constraint into the spinner part, change the actuator type to ‘motor’ and an angular velocity property should appear.
Make sure one attachment is in the part itself and another is behind the part.
If the attachments are in a line they draw out where a hinge would be, and it’ll spin on that axis.
[make sure both attachment parts have can collide off, and check ‘massless’ if it stutters.]
@victoriazones what @Irideon said is true and is one of multiple ways to rotate a part. I’ve attached the file. In the example below, you can see another solution, which involves the use of CFrames and setting primary part of the model. It runs pretty smoothly, doesn’t require any aditional attachments and is just as easily achieved. However, there is a small drawback to this method: the fact that assembled parts slightly drift away from each other over a time period of active rotation. Luckly, the model I used consists of only two parts, so my tests at high speeds showed some positive results.
Here is a very good as well as helpful and comprehensive post by@Corecii , who has covered pretty much every single one of various ways to rotate a part. They also described the bug I mentioned above.
You can now choose from any of these methods.
I. Primary part solution
... which seems to not be totally ideal solution, but works well and is among the easiest ones:
--[[
lightModel includes light part, rotational point and
a surface light. See the picture below.
]]
local lightModel = script.Parent.Model
local lightSource = lightModel.Light.SurfaceLight
local detector = script.Parent.ClickDetector
-- set default position and orientation
local ROOT_POINT_CF = lightModel.RotPoint.CFrame
local RATE = .6 -- rotational speed rate
local angle = 0
local spin = false
lightSource.Enabled = false
coroutine.wrap(function()
while (true) do
if (spin) then
lightModel:SetPrimaryPartCFrame(
ROOT_POINT_CF * CFrame.Angles(angle, 0, 0)
)
--[[
1) Back to zero after each turn.
2) math.pi is two times faster than math.rad()*
*units are radians; 2*π = 360°
]]
angle = (angle + RATE) % (2* math.pi)
end
wait()
end
end)()
detector.MouseClick:Connect(function()
spin = not spin
lightSource.Enabled = not lightSource.Enabled
end)
You can freely customize properties, as long as you keep all the parts anchored.
Quite simple.
Components: a rotating model with defined primary part, more coding.
II. HingeConstraints
... and the attachments that come with them:
local light = script.Parent.Light
local lightSource = light.SurfaceLight
local detector = script.Parent.ClickDetector
local VELOCITY = 5
local spin = false
lightSource.Enabled = false
light.HingeConstraint.MotorMaxTorque = 50000
light.HingeConstraint.AngularVelocity = 0
detector.MouseClick:Connect(function()
spin = not spin
lightSource.Enabled = not lightSource.Enabled
if (spin) then
light.HingeConstraint.AngularVelocity = VELOCITY
else
light.HingeConstraint.AngularVelocity = 0
end
end)
Time to complete: similar.
Components: more instances (HingeConstraint, two attachments), less coding.
Alarm files: ------------ after downloading open by dragging in workspace!
DON’T FORGET TO CLICK ON THE ALARM IN ORDER TO START AND STOP IT!