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

Is there a better way to end a script without destroying it?

Asked by 4 years ago
Edited 4 years ago

Code that I need help with:

1script:Clone().Parent = script.Parent
2wait()
3script:Destroy()

Full code:

01game.Players.PlayerAdded:Connect(function(plr)
02    wait(50)
03    local teams = game:GetService("Teams"):GetTeams()
04    local teamended
05    local OutOfRound = {}
06    local teamio
07    plr.Character:WaitForChild("Humanoid").Died:Connect(function()
08        wait()
09        table.insert(OutOfRound, plr.Name)
10        local function dothedo()
11            for _, team in pairs(teams) do
12                local players = team:GetChildren()
13                    for i = 0, #players do
14                    if players[i] ~= OutOfRound[i] then
15                        else
View all 54 lines...

Is there a better way? I know disabling it is just downright stupid.

For context while 1>0 do is the same as while true do, and my code works.

0
i don't really know what you mean, but you can use return i guess Gmorcad12345 434 — 4y

3 answers

Log in to vote
0
Answered by
MediaHQ 53
4 years ago
Edited 4 years ago

If you want to you can disable it then re-enable it to restart it.instead of cloning it and removing the original

1script.Disabled = true
2script.Disabled = false --This restarts the script

This might have not answered your question but still a solution

0
No, I literally said " I know disabling it is just downright stupid." WideSteal321 773 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Hm, it's quite strange what you are doing. I wouldn't recommend cloning and destroying scripts as things can get confusing and connections can be spammed (overall slowing your game down). If you give me more details, I could suggest a more efficient approach.


break

I believe you are looking for break, which is a way to exit a loop. You can also put it in your while (condition) do for it to break, but it looks like you are doing calculations after (in which you can use repeat until.

01while true do
02    if (teamended == "SG") then
03 
04    end
05    if (teamended ~= "") then
06 
07    else
08        break --this will basically end the loop
09    end
10end

:Disconnect()

Connections can still be intact after a script is destroyed (please correct me if I'm wrong). If your intention is to destroy a script, you want to disconnect events before destroying them.

1Connection = Players.PlayerAdded:Connect(function(player)
2    Connection:Disconnect()
3    -- by disconnecting, we no longer listen for players who join
4    -- this will stop "new" code from running
5    -- as well as prevent connection spams
6    script:Destroy()
7end)
0
I still want the script to run after "destroying it" WideSteal321 773 — 4y
0
Is there a specific reason for this interaction? I feel like you can approach this in a completely different manner and get the same but better results... PhantomVisual 992 — 4y
Log in to vote
0
Answered by 4 years ago

Instead of script:Destroy(), do script.Disabled = true. This will disable the script so it will no longer activate. To enable it again, type: script.Disabled = false. The script will now be enabled. Hope this helps <3

0
I literally said " I know disabling it is just downright stupid." WideSteal321 773 — 4y

Answer this question