Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make a sound only play once in a while loop?

Asked by 1 year ago

Im trying to make it where a sound plays only once whent he lights turn on/off, but since the program needs to keep checking if the lights are on or off, it also constantly plays the sound. I've tried solving this by placing a separate while loop that detects what time of day it is and when to play the sound. Any help?

while true do
    lighting.ClockTime = currenttime

    for _, Lamp in ipairs(Lamps) do


        if (currenttime >= 6 and currenttime <= 19) then
            Lamp.Material = Enum.Material.Plastic
            Lamp.PointLight.Enabled = false




        else
            Lamp.Material = Enum.Material.Neon
            Lamp.PointLight.Enabled = true


        end


    end
    currenttime += .01
    if currenttime >= 24 then
        currenttime = 0
    end
    wait()
end
while true do
    for _, Lamp in ipairs(Lamps) do
        if (currenttime >= 6 and currenttime <= 7) then 
            Lamp.LightsOff:Play()
    end

    end
end

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

I recommend using a BoolValue or an attribute since they have an event that only fires whenever their value (true/false) changes, which is REALLY helpful for your script.

Use my answer from an old question that has the similar problem, don't worry I explained everything there.

local Lamps = workspace.Lamps -- if the script doesn't work EDIT THIS!!!!!
local Lighting = game:GetService("Lighting")

local startOfDay = 6
local startOfNight = 18

local function checkIfDaytime(clockTime)
    if (clockTime >= startOfDay) and (clockTime < startOfNight) then -- if it's daytime
        return true
    elseif (clockTime >= startOfNight) or (clockTime < startOfDay) then -- if it's nighttime
        return false
    end
end

script:SetAttribute("Daytime", checkIfDaytime(Lighting.ClockTime)) -- creates the attribute
if script:GetAttribute("Daytime") == true then
    for _, Lamp in ipairs(Lamps) do
        Lamp.Material = Enum.Material.SmoothPlastic
        Lamp.PointLight.Enabled = false
        Lamp.LightsOff:Play() -- plays the sound
    end
else
    for _, Lamp in ipairs(Lamps) do
        Lamp.Material = Enum.Material.Neon
        Lamp.PointLight.Enabled = true
    end
end

script:GetAttributeChangedSignal("Daytime"):Connect(function() -- when the attribute changes
    local attribute = script:GetAttribute("Daytime")
    if attribute == true then -- if it's true (day)
        for _, Lamp in ipairs(Lamps) do
            Lamp.Material = Enum.Material.SmoothPlastic
            Lamp.PointLight.Enabled = false
            Lamp.LightsOff:Play()
        end
    else -- if it's false (night)
        for _, Lamp in ipairs(Lamps) do
            Lamp.Material = Enum.Material.Neon
            Lamp.PointLight.Enabled = true
        end
    end
end)

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function() -- when the clock changes
    local clockTime = checkIfDaytime(Lighting.ClockTime)

    script:SetAttribute("Daytime", clockTime) -- changes the attribute
end)

while true do
    Lighting.ClockTime += 0.1 -- changes the clock
    if Lighting.ClockTime >= 24 then
        Lighting.ClockTime = 0
    end

    task.wait()
end
Ad

Answer this question