luarobloxroblox-studio

Refreshing script in roblox


I'm writing a scripts for a concert in roblox. There is a script in a ServerScriptService, that controls all music and lights. I need a gui reset button, that will start this script from the beginning.

I have done something like this:

local button = script.Parent
local ServerScript = game:GetService("ServerScriptService")
local ScreenGui = game:GetService("StarterGui").ScreenGui


local function onButtonActivated()

    for _, sound in ipairs(workspace.Sounds:GetChildren()) do
        if sound:IsA("Sound") and sound.Playing then
            sound.Playing = false
        end
    end
    
    for _, scripts in ipairs(ServerScript.Sounds:GetChildren()) do
        if scripts.Enabled then 
            scripts.Enabled = false
            
        end
    end
    
end


button.MouseButton1Click:Connect(onButtonActivated)

My code really stops the music and lights, but if I call the script again it continues playing from the place it has stopped, not from the beginnig. How to fix that?


Solution

  • Use sound:Play() and sound:Stop() instead of using sound.Playing.

    sound:Stop() resets the sound to the beginning of the track instead of wherever it currently is.

    You can use :Resume() and :Pause() respectively to start and stop at the current position.

    Documentation for the Sound class