I have this script for a streetlight that works perfectly. The light turns on after dark, and turns off during day. How would I change this to work for a sound in unisom with the light? The sounds name is “Buzzz”. Light script here:
local lightPart = script.Parent
local pointlight = lightPart.PointLight
while true do
wait (0.1)
if game.Lighting:GetMinutesAfterMidnight()> 6 * 60 then
lightPart.Material = Enum.Material.Plastic
pointlight.Enabled = false
end
if game.Lighting:GetMinutesAfterMidnight()> 18 * 60 then
lightPart.Material = Enum.Material.Neon
pointlight.Enabled = true
end
end
If the sound is set to loop, you could check if the sound is playing under the if-statement for when it’s nighttime, calling :Play() on it if it’s not (calling :Play() every time will make it restart and sound choppy). In the if-statement for when it’s daytime, you could :Stop() the sound if its currently playing.
local sound = script.Sound
--Stop a playing sound.
if sound.IsPlaying then
sound:Stop()
end
--Start a stopped sound.
if not sound.IsPlaying then
sound:Play()
end
Assuming the sound is looped…
(have not tested, should work however)
--< Better to use events than loops
local lightPart = script.Parent;
local pointlight = lightPart.PointLight;
local lighting = game.Lighting;
local sound = lightPart:WaitForChild("Buzzz", 5); -- Wait for 5 seconds for "Buzzz"
if not sound then print("Buzzz is not a valid child of lightPart. Please make sure the sound exists."); return end;
local switch = 0; -- Allows the code to run once to update the lights when needed, rather than updating every time the time changes
lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
local timeAfter = lighting.ClockTime * 60; -- Convert to minutes after to be compatible with your existing code
if(timeAfter > 6 * 60) then
if(switch ~= 0) then return end;
switch = 1;
if(sound.Playing) then -- Is the sound playing?
sound:Stop(); -- If yes, stop
end
lightPart.Material = Enum.Material.Plastic;
pointLight.Enabled = false;
elseif(timeAfter > 18 * 60) then
if(switch ~= 1) then return end;
switch = 0;
if(not sound.Playing) then -- Is the sound playing?
sound:Play(); -- If not, play
end
lightPart.Material = Enum.Material.Neon;
pointLight.Enabled = true;
end
end)
local lightPart = script.Parent
local pointlight = lightPart.PointLight
local buzzz = script.Buzzz
buzzz.Looped = true
while true do
wait (0.1)
if game.Lighting:GetMinutesAfterMidnight()> 6 * 60 then
lightPart.Material = Enum.Material.Plastic
pointlight.Enabled = false
if not buzzz.IsPlaying then
buzzz:Play()
end
end
if game.Lighting:GetMinutesAfterMidnight()> 18 * 60 then
if buzzz.IsPlaying then
buzzz:Stop()
end
lightPart.Material = Enum.Material.Neon
pointlight.Enabled = true
end
end