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

Recreating a Destroyed Object with working click detector?

Ok I'm a bit of a noob here so forgive my ignorance but I have a question...

I have a multi-part model. There's a click detector on the model. When it's clicked it explodes and the parts/model are destroyed That part is working.

Now I'd like to recreate it after a few seconds.

To do this I'm cloning the model prior to the explosion. Then after a wait, I create a model from the clone. .. which works... but "new" instance of the model doesn't respond to the clickdetector. Even though the model has one. So I can't blow up the new one.

Is there something special I need to do to make my MouseClick connect function respond to clicks on the new model that was built from the clone?

1 comment

0

You need to connect a new event each time you make the clone which included the new clickdetector. Using Destroy() on the existing model as it will also disconnect all events.

2 answers

There's multiple alternatives.

such as, making the transparency of the exploded brick to 1, cloning and then moving them,

But in this case, you before exploding it; you can clone them somewhere, and then moving it back into the clickdetector. (depends on your script)

as you said, "Even though the model has one. So I can't blow up the new one."

Make sure the part has the same exact name as the one in the explosion script, also add a :WaitForChild() function

script.Parent.MouseClick:connect(function()
brick = script.Parent.Parent.Part -- assuming that the part is in a model

brick:Clone().Parent = game.ServerStorage -- or clone it anywhere you want,
-- explosion script here
wait(3) --after a few seconds
if game.ServerStorage:FindFirstChild("Part") then
game.ServerStorage.Part.Parent = script.Parent.Parent
if game.ServerStorage:FindFirstChild("Part") == nil then -- just some printing to check
print("part doesnt exist in serverstorage")
end
end
end)

Another alternative is to just create a new instance.

script.Parent.MouseClick:connect(function()

-- explosion script here

part = Instance.new("Part") -- start configuring all properties here
part.Anchored = true
part.Name = "Part"
part.BrickColor = BrickColor.new("Really red")
part.Parent = script.Parent.Parent
end
end)

Also make sure you clone it BEFORE you destroy it.

1 comment

0

Thanks so much - I'm gonna post new reply so I can include some code snippets.

Thanks so @ast_raa much I'll work with this asap. A couple of follow up thoughts/questions.

You said:

"But in this case, you before exploding it; you can clone them somewhere, and then moving it back into the clickdetector. (depends on your script)"

I am cloning before destroying but I'm not rebuilding it until after the explosion. Also I'm not sure what you mean by "moving it back into the clickdetector" my click detector is in the model ... and thus in the clone.

Here's my code:


local backup = game.Workspace.TNT:clone() -- Make the backup	clone	
local newTNT = script.Parent  -- Define a new one


--[[ BEGIN : Blowing Stuff up ]]--
		
local TNTexplosion = Instance.new("Explosion")
TNTexplosion.Position = game.Workspace.TNT.band.Position
TNTexplosion.Parent = game.Workspace.TNT
					
for index, object in pairs(game.Workspace.TNT:GetChildren()) do 		
	if object:IsA("BasePart") then 
		object:Destroy()  -- Destroy the parts within the model
	end				          
end

wait(0.2)
	
game.Workspace.TNT:Destroy() -- Destroy the model

--[[ END: Blowing Stuff up ]]--	
		

--[[ BEGIN : See if they player is Dead ]]--

local deadCheck = player.Character.Humanoid.Health

	if deadCheck == 0 then
		msgis = player.Name .. " went BOOM!"
	else
		msgis = "Be Careful " .. player.Name .. "!"
	end

g2penvt:FireServer(player,msgis) -- Send the message to the player's screen

--[[ END  See if they player is Dead ]]--


--[[ BEGIN : Recreate the TNT ]]--

wait(3) -- Don't rebuild the TNT right away...

newTNT = backup:clone() 
newTNT.Parent = game.Workspace 
newTNT:makeJoints()		

--[[ END: Recreate the TNT ]]--

The re-created (clone) model that is created after the wait does appear to be named the same.

It HAS the click detector just as the origional ... my click listener function just doesn't seem to be able to "hear" the click event from the new one.

I'll look into the :WaitForChild() function. I'm not totally clear on what that will do or where to put it but I'll read up on it.

1 comment

0

line 43, you're cloning it twice. check line 1, you've already cloned that. "game.Workspace.TNT:Clone():Clone()" wont work, last time i checked. Also, ROBLOX is case sensitive, make sure it's the "Clone()"

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