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

How do I stop a script after it has started running?

Asked by 7 years ago

I have made a function that is supposed to play a song and display a message, stop any music that is playing by stopping the sounds in the SoundScape, (which is where I normally put songs) clone then destroy the script that plays the sounds so that it doesn't play any other songs, and afterwards set the cloned script in the work space so that it can run again. This function is supposed to fire when a specific player joins the server and their character finishes loading.

local music = game.Workspace.Music
local creators = {
    "SparkingFruitPunch", "Player2"
}
local already = {}

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function()
        for i,v in pairs(creators) do
        if v == plr.Name and not already[plr.Name] then
            already[plr.Name] = true
            local sounds = game.Soundscape:GetChildren()
            for i = 1, #sounds do
                sounds[i].Playing = false
            end
            local mc = game.Workspace.Music:Clone()
            mc.Parent = game.ServerStorage
            music:Destroy()
            local nega = Instance.new("Sound", game.Workspace)
            nega.SoundId = "rbxassetid://440129799"
            nega.TimePosition = 43.00
            nega.Volume = 1
            nega:Play()
            local m = Instance.new("Message", game.Workspace)
            m.Text = "The creator, "..plr.Name.." has shown himself upon this day!"
            wait(5.30)
            m:Destroy()
            nega:Destroy()
            mc.Parent = game.Workspace
        end
    end
    end)
end)

PLEASE KEEP ALL OF THESE PROBLEMS IN MIND WHEN COMING UP WITH A SOLUTION:

  1. I've recently found out that once a script starts running, you can't simply stop it by disabling it, and this script proves that you can't do it by destroying it either (The variable "music" is pointing to a script). How would I stop a script from running?

  2. On an actual server, the music does not stop playing. This is strange because inside the function is a for loop that goes through every audio file and makes sure that they're not playing.

  3. This is just a hypothesis, but i'm pretty sure if the specified player rejoins the server, this script would error because it has already destroyed the original music script, and the function would ask to destroy it again.

Again, please keep all of these problems in mind when thinking up a solution, because lately people haven't been doing that. (For me at least) Here's the music script if you need it:

local sonic = game.Soundscape.specialStage
local sonicw = sonic.TimeLength
local dbz = game.Soundscape.SoHappy
local dbzw = dbz.TimeLength
local sponge = game.Soundscape.SpongeArcade
local spongew = sponge.TimeLength
local x4 = game.Soundscape.x4
local x4w = x4.TimeLength

while true do
    x4:Play()
    wait(x4w)
    sonic:Play()
    wait(sonicw)
    sonic:Stop()
    wait(5)
    dbz:Play()
    wait(dbzw)
    dbz:Stop()
    wait(5)
    sponge:Play()
    wait(spongew)
    sponge:Stop()
    wait(5)
end
0
adda Lolamtic 63 — 7y
0
add a debounce that never ends so it will never end at the end :§ Lolamtic 63 — 7y

1 answer

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
7 years ago
Edited 7 years ago

Before I start: Don't fully assume something isn't possible

You obviously don't know some of the few ways to stop a script or even a loop. These include return and break. Aswell as using a Script's Disabled property:


Using return

Return is neat to use when wanting to simply return a value or maybe even get out of a function. I like to use this often with debounces. Here's more on using a debounce.

--// Say I wanted to make it so the player can only click once ever 5 seconds

local debounce = false --Making a local debounce

mouse.Button1Down:connect(function()
    if debounce == true then --If my debounce is true then
        return --Return out of this function entirely. We stop the rest from running.
    end

    debounce = true --Usually goes after the debounce if statement

    --If debounce is false then we can continue with printing...
    print("The mouse was clicked!")

    wait(5) --waiting
    debounce = false -- Setting the debounce so we can enable the player to re-click
end)

Using break

Like return, break is really simple to use in the same nature of a debounce except they're only used for your while loops, for loops, and your repeat loops. More on using break with loops.

--// You can set this up like a debounce but here's a little copy/paste off wiki on breaks.

while wait() do
   print("hi mom!")
   break -- this forces the endless loop to end
end 

Your script also seems to be missing things that solve your music problem. You seem to be forgetting to stop your music before destroying it in the first script provided.

Line 26:

wait(5.30)
m:Destroy()

--Adding the :Stop()
nega:Stop()
nega:Destroy()

You can Disable a script that is running at any time by using it's Disabled property. By default, Scripts that are inserted have that property set to false so it's basically always going to start running. If you want to disable it then you can just add YourScript.Disabled = true some where. Setting this back to false after doing this will restart the script.

For your 3rd "Problems to keep in mind". You can easily get out of this situation by cloning the music script and putting it into the player every time they join. Put this in your game.Players.PlayersAdded function. You'll then have the ability to disable it whenever you want.

If you need more elaboration on implementing this into your code then please freely reply on this post.

0
I would like some more elaboration, thank you. Also, I already knew about the disabled property - I even mentioned it in the first problem. The music that I was trying to stop was not the song that was created with Instance.new, but the audio files in "SoundScape." There is a for loop within the function that is supposed to do that. SparkingFruitPunch 4 — 7y
0
I did some more testing and discovered that the script DID in fact, stop running, but what I don't understand is why the current song didn't stop playing. I've burnt myself out from thinking too much, so please keep the elaboration as simple as you can. SparkingFruitPunch 4 — 7y
Ad

Answer this question