Model would not regen with a button after falling into void

Hello. I was revamping the button to spawn the train in my game. When the model falls into the void, the button refuses to spawn the model.
Script:

local cooldown = 5

local button = script.Parent
local click = button.activate
local MainModel = script.Parent.Parent.Model
local backup = MainModel:Clone()

backup.Parent = game.ServerStorage
button.Color = Color3.new(0, 0.615686, 1)

click.MouseClick:Connect(function()
	local modelclone = backup:Clone()
	button.Color = Color3.new(0.207843, 0.207843, 0.207843)
	for _, maincontents in pairs(MainModel:GetChildren()) do
		maincontents:Destroy()
	end
	click.Parent = game.ServerStorage
	wait(0.5)
	modelclone.Parent = MainModel
	wait(cooldown)
	button.Color = Color3.new(0, 0.615686, 1)
	click.Parent = button
end)

Your code doesn’t make sense, why parent the clone into the main model? When all of the children of the model are destroyed, it doesn’t work anymore.

Instead just… put the clone in workspace.

1 Like

So it can be regenerated again.

Yeah, I agree that the code makes no sense.
First where is the script located? it is not destroyed along with the model?

Then.

  • You reference this under variable MainModel: script.Parent.Parent.Model
  • Then you clone MainModel and name it backup. You placed this in SS
  • Then you clone backup and place it in MainModel… is the Model still in workspace? or its destroyed cause it fell to void?

The script seems that is inside the model due to script.Parent.Parent.Model. If the Model is in workspace, you could reference it game.Workspace.Train instead of doing Parent.Parent.Parent.Parent…

And seems you are cloning backup model, to place it inside the model that got destroyed.

You should change entire cloning logic.

image
(regenpart is the button)

You’re whole clone script is overcomplicated. But here’s your answer.

Change this:

To this:

modelclone.Parent = game.Workspace

The problem is whenever MainModel gets destroyed, you can no longer place modelclone in MainModel. If you open your output log you should be getting errors warning you about this.

This is also what the other two replies are trying tell you.

1 Like

Ok, but I want to clean the previous model regenerated when you press the button again.
EDIT: ok nvm i found out

	for _, maincontents in pairs(MainModel:GetChildren()) do
		if maincontents == nil then
			continue
		else
			maincontents:Destroy()
		end
	end

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