Getting back a script after "script:Destroy()" with another script

  1. I have thought of an idea about getting a script back after it destroyed itself.

  2. The script should appear after another script has been activated.

    (and then this script should get destroyed and so on)
    Script1 destroys and creates Script2
    Script2 destroys and creates Script1

  3. I have tried Disabling and Enabling but Scripts1 keeps running while Script2 has been activated by Script1.

    I could make a Storage where a Clone is inside and replacing the older once.
    But Idk how to make one. *me dummy : )

  • (I have read through interesting Forums and other sites but nothing helped.)

Edit: Any other idea would help learn something new.
Read complete post to understand where we left off!

4 Likes

You could just make it so that Script1 is the parent of Script2. Script2 has .Enabled set to false, and Script 1 clones Script2 and does:

clone.Parent = script.Parent
local thisClone = script:Clone()
thisClone.Enabled = false
thisClone.Parent = clone
clone.Enabled = true

Then you can make clone of Script2 enable the clone of Script1 (now in the clone of Script2)

Hope this was what you were looking for!

4 Likes

Great idea, I will try it out.

2 Likes

It doesn’t work for me and “clone” got a warning maybe because it has no event.

Let’s say I make a part which has a Clickdetector and cycles the scripts back and forward.
(Like you said Script1 is the parent of Script2 and if the part was clicked they swap places over and over.)

I got a small code written for Clickdetector:

local part = script.Parent
local click = part.ClickDetector

local function onClick(###) --Some name inside
	-- whatever happens here
end

click.MouseClick:Connect(onClick)

That would help understand it better and using it in future.

2 Likes

I got it working only once, then it got stuck and has an error.

Here is the code for Script1:

local part = script.Parent
local click = part.ClickDetector
local script1 = script
local script2 = script.Script2

local function onClick(clone)
	local clonedscript2 = script2:Clone()
	clonedscript2.Parent = script.Parent
	clonedscript2.Name = "Script2"
	clonedscript2.Enabled = true
	
	local clonedscript1 = script1:Clone()
	clonedscript1.Parent = script.Parent.Script2
	clonedscript1.Name = "Script1"
	clonedscript1.Enabled = false
	
	part.Color = Color3.fromRGB(36, 255, 24)
	
	wait(2)
	
	part.Color = Color3.fromRGB(163, 162, 165)
	
	script:Destroy()
end

click.MouseClick:Connect(onClick)

Here is the code for Script2:

local part = script.Parent
local click = part.ClickDetector
local script2 = script
local script1 = script.Script1

local function onClick(clone)
	local clonedscript1 = script1:Clone()
	clonedscript1.Parent = script.Parent
	clonedscript1.Name = "Script1"
	clonedscript1.Enabled = true

	local clonedscript2 = script2:Clone()
	clonedscript2.Parent = script.Parent.Script1
	clonedscript2.Name = "Script2"
	clonedscript2.Enabled = false
	
	part.Color = Color3.fromRGB(36, 255, 24)

	wait(2)

	part.Color = Color3.fromRGB(163, 162, 165)
	
	script:Destroy()
	
end

click.MouseClick:Connect(onClick)

How my Explorer is looking inside Workspace:
image
And lastly the Error:
ClickDetector is not a valid member of Script “Workspace.TestPart.Script2”

1 Like

Try setting the variable part from script.Parent to

workspace.something.something.something.TestPart:WaitForChild("ClickDetector")

(Obviously, replace the gibberish I wrote with your path.)

2 Likes
local target = --path to your script
--assuming the parent remains the same and won't be destroyed, else the script will get more complicated
local parent = target.Parent

function onDestroy()
	target = target:Clone()
	--art of recursion
	target.Destroying:Once(onDestroy)
	target.Parent = parent
end

target.Destroying:Once(onDestroy)

This works within the script as well:

--self reviving script, prints "hello" every 2 seconds
script.Destroying:Once(function()
	script:Clone().Parent = script.Parent
end)

--basically your code goes here
print("hello")
task.wait(2)
script:Destroy()

Keep in mind these scripts don’t handle script.Parent = nil, script.Enabled = false, script.Disabled = true and script:Remove()(because remove sets the parent of the instance and all its descendants to nil). Although it handles game.Debris:AddItem(script) because I assume it either calls Destroy() internally or fires the Destroying event manually copying the behavior of the destroy function.

3 Likes

I tried this script out:

And it’s awesome! A self reviving script, thanks for that.
Now I try to work out a second script that has a different code than the first one.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.