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

How to make a object regenerate when destroyed with a click detector?

--When a object is destroyed it will wait 2 or more seconds and regenerate in the same spot. I've tried cloning the object but it wont work is anything wrong with my script? If there is please tell me!


if

   script.Parent.ClickDetector.MouseClick:connect(function()
	script.Parent:Destroy()	
end)

then 

script.Parent.Clone()
		
end

2 answers

You have to parent the clone to workspace after cloning the part. Also, use : rather than . when calling functions such as Clone() or Destroy()

Here's what it should look like:

local thing = script.Parent
script.Parent = nil

local copy = thing:Clone()

local Reconnect
Reconnect = function()
	thing.ClickDetector.MouseClick:Connect(function()
		thing:Destroy()
		wait(2)
		thing = copy:Clone()
		thing.Parent = workspace
		Reconnect() --recursion
	end)
end

Reconnect()

put the part that you want to be respawned into replicated storage in the position you want it to be respawned in (if you want it to appear in different places move its position using PrimaryPart, which will have to be set..)

then use this script inside the respawn button which also has a click detector:

local respawn_button = script.Parent.ClickDetector

local function respawn()
	local original = game.workspace:FindFirstChild("model name here")
	original:Destroy() 
	local model = game.ReplicatedStorage:FindFirstChild("model name here"):Clone()
	model.Parent = game.workspace
end

respawn_button.MouseButton1Click:connect(respawn)

respawn()

Think this should work but I've just typed it here and didnt test it in studio :P

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