Hey, I am trying to code a button that connects to other lights and makes them flash and has a lot of other actions along with it can somebody explain how I can do this?
Are you trying to create a UI button, or a physical button in the Workspace? If you’re going for a click button in the Workspace, you’ll need to use a ClickDetector with a function that fires every time the button is clicked. But if you’re going for a button in the player GUI, you’ll need to set up a remote connection that will allow the text/image button to fire an event and make any changes in the Workspace.
I would recommend searching up information and tutorials with buttons on YouTube and on the developer hub.
Here’s some code, put it in your button part.
Make sure you create a model that contains all your light parts. Set the variable “lights” to that model (sample provided)
I commented every line so you can learn what each piece does.
-- (untested)
-- put this code within a Script inside your button part
local button = script.Parent -- reference to the script's Parent, which should be the button itself
local lights = workspace.YourLightsModel -- put your parts that contain alarm lights into a model, reference it here
local cd = Instance.new("ClickDetector") -- make a new ClickDetector
cd.Parent = button -- put the ClickDetector in the button so we can detect player clicks
local active = false -- whether or not the alarm is active
local coolDown = 1 -- the frequency at which the alarm can be toggled (in seconds)
local lastToggle = 0 -- when the alarm was last toggled (set to tick())
local onColor,offColor = Color3.fromRGB(170,255,0),Color3.fromRGB(255,0,0) -- Green, Red
ToggleLights = function(bool) -- bool is either true or false
for _,light in next,obj:GetDescendants() do -- iterate over all descendants of the lights container model
if light:IsA("PointLight") or light:IsA("SpotLight") then -- if it's a light of any type, proceed
light.Enabled = bool -- set the light's Enabled property to the value of "bool"
end
end
end
cd.MouseClick:connect(function(player) -- when the ClickDetector gets clicked, run this unnamed function
if (tick() - lastToggle) > coolDown then -- enough time has passed since last toggle
lastToggle = tick() -- set the last toggle to the current time
active = not active -- switches the value of the bool
ToggleLights(active) -- switch the lights
button.Color = (active and onColor) or offColor -- switch the button color
-- put your other effects here
end
end)
EDIT: After re-reading the OP, you specified that you wanted flashing lights. The script below will do that using coroutines. The script above will simply turn the lights on during an alarm.
-- (untested)
-- put this code within a Script inside your button part
local button = script.Parent -- reference to the script's Parent, which should be the button itself
local lights = workspace.YourLightsModel -- put your parts that contain alarm lights into a model, reference it here
local cd = Instance.new("ClickDetector") -- make a new ClickDetector
cd.Parent = button -- put the ClickDetector in the button so we can detect player clicks
local active = false -- whether or not the alarm is active
local coolDown = 1 -- the frequency at which the alarm can be toggled (in seconds)
local lastToggle = 0 -- when the alarm was last toggled (set to tick())
local onColor,offColor = Color3.fromRGB(170,255,0),Color3.fromRGB(255,0,0) -- Green, Red
ToggleLights = function(bool)
for _,light in next,obj:GetDescendants() do -- iterate over all descendants of the lights container model
if light:IsA("PointLight") or light:IsA("SpotLight") then -- if it's a light of any type, proceed
light.Enabled = bool -- set the light's Enabled property to the value of "bool"
end
end
end
FlashSequence = coroutine.wrap(function() -- coroutines run synchronously and won't yield the rest of your thread, which means this can happen while you perform other effects
local bool = false
while active do -- while active is true, loop the following
bool = not bool -- flip the value of bool
ToggleLights(bool) -- toggle lights according to bool
wait(1/2) -- wait half a second, the lower the number, the faster the flashing
end -- end of loop
ToggleLights(false) -- the loop has broken, turn all lights off
end)
cd.MouseClick:connect(function(player) -- when the ClickDetector gets clicked, run this unnamed
if (tick() - lastToggle) > coolDown then -- enough time has passed since last toggle
lastToggle = tick() -- set the last toggle to the current time
active = not active -- switches the value of the bool
FlashSequence() -- begin the flash sequence
button.Color = (active and onColor) or offColor -- switch the button color
-- put your other effects here
end
end)
Your best way, is to link the spinning and flashing to a script and a global value they would read from. If the value is true, they would spin + flash if false, they would stop flash and spin.
Well here is something:
local button = script.Parent
local part = workspace.Part
local sound = part.Sound
local light = part.Light
button.MouseButton1Click:Connect(function()
light.Color = Color3.new(0, 0, 0) -- change to what colour
sound:Play()
end)
button.MouseButton2Click:Connect(function()
light.Color = Color3.new(0, 0, 0) -- change to what colour
sound:Stop()
end)
It’s very basic and its not tested but I hope this helps you.