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.
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
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
.
while true do if (teamended == "SG") then end if (teamended ~= "") then else break --this will basically end the loop end 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.
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)
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