Code that I need help with:
1 | script:Clone().Parent = script.Parent |
2 | wait() |
3 | script:Destroy() |
Full code:
01 | game.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 |
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.
If you want to you can disable it then re-enable it to restart it.instead of cloning it and removing the original
1 | script.Disabled = true |
2 | script.Disabled = false --This restarts the script |
This might have not answered your question but still a solution
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.
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
.
01 | while 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 |
10 | end |
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.
1 | Connection = 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() |
7 | end ) |
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