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 3 years ago
Edited 3 years ago

Code that I need help with:

script:Clone().Parent = script.Parent
wait()
script:Destroy()

Full code:

game.Players.PlayerAdded:Connect(function(plr)
    wait(50)
    local teams = game:GetService("Teams"):GetTeams()
    local teamended
    local OutOfRound = {}
    local teamio 
    plr.Character:WaitForChild("Humanoid").Died:Connect(function()
        wait()
        table.insert(OutOfRound, plr.Name)
        local function dothedo()
            for _, team in pairs(teams) do
                local players = team:GetChildren()
                    for i = 0, #players do
                    if players[i] ~= OutOfRound[i] then
                        else
                        teamio = false
                        print(teamio)
                        end
                    end
            if teamio == false and team.Name == "Killer Bass" then
                teamended = "KB"
            elseif teamio == false and team.Name == "Screaming Gophers" then
                teamended = "SG"

            else
                wait(1)
                    print(teamio)
                end
            end
        end
        dothedo()
        while 1>0 do
            wait(2)
            if teamended == "SG" then
                wait()
                game.ReplicatedStorage.SGLost:FireAllClients()
                print(1)
            elseif teamended == "KB" then
                wait()
                game.ReplicatedStorage.KBLost:FireAllClients()
                print(1)
            else
                print(1)
            end
            if teamended ~= "" or nil then
                dothedo()
            else
                script:Clone().Parent = script.Parent
                wait()
                script:Destroy()
            end
        end
    end)
end)

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 — 3y

3 answers

Log in to vote
0
Answered by
MediaHQ 53
3 years ago
Edited 3 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

script.Disabled = true
script.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 — 3y
Ad
Log in to vote
0
Answered by 3 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.

while true do
    if (teamended == "SG") then

    end
    if (teamended ~= "") then

    else
        break --this will basically end the loop
    end
end

: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.

Connection = Players.PlayerAdded:Connect(function(player)
    Connection:Disconnect()
    -- by disconnecting, we no longer listen for players who join
    -- this will stop "new" code from running
    -- as well as prevent connection spams
    script:Destroy()
end)
0
I still want the script to run after "destroying it" WideSteal321 773 — 3y
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 — 3y
Log in to vote
0
Answered by 3 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 — 3y

Answer this question