Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

1 comment

0

i don't really know what you mean, but you can use return i guess

3 answers

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

1 comment

0

No, I literally said " I know disabling it is just downright stupid."

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)

2 comments

0

I still want the script to run after "destroying it"

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

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

1 comment

0

I literally said " I know disabling it is just downright stupid."

This discussion is preserved as it was. New answers, comments, and votes are closed.