Respawning parts

I am currently working on a money system for my game so if the player clicks a stack of money, it adds an amount to the leaderboard, then destroys the part and respawns. I would like to know if you can destroy a part and make that part come back/respawn.

This is my script currently for the stack of money. If a part can be respawned after it has been destroyed, I want to know what to add after wait(10) in order for the part to respawn.

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local stat = player.leaderstats.Dollars
	stat.Value = stat.Value + 100
	script.Parent:Destroy()
	wait(10)
	
end)

2 Likes

You could just create a variable for the Part outside the MouseClick event, and set it to nil so you can save it for later after 10 seconds pass to parent it back to the workspace

local Part = script.Parent

Part.ClickDetector.MouseClick:Connect(function(player)
	local stat = player.leaderstats.Dollars
	stat.Value = stat.Value + 100
	Part = nil
	wait(10)
	Part = workspace
end)
1 Like

Alright so I can give you a good example of a spawning system, here we go!


Example

Alright, so i’m going to use my own map spawning system as an example.
As you can see, I have a folder in workspace and a folder in ServerStorage that holds these said models.

--You'll have to customize this to fit your needs.
function Spawner(plr,Function,Folder,MatchName)
	local Storage = game.ServerStorage.Matchtypes
	local Matchtypes = game.Workspace.Matchtypes
	if Function == "Spawn" then
		if not Matchtypes[Folder]:FindFirstChild(MatchName) then
			if Storage:FindFirstChild(Folder) then
				if Storage[Folder]:FindFirstChild(MatchName) then
					Storage[Folder][MatchName]:Clone().Parent = Matchtypes[Folder]
				end
			end
		end
	elseif Function == "Destroy" then
		if Matchtypes[Folder]:FindFirstChild(MatchName) then
			Matchtypes[Folder][MatchName]:Destroy()
		end
	end
end

How this works

Quite simple; it checks to see if it’s already spawned in the workspace folder and if not, it spawns one from ServerStorage. It’s controlled through a remote event but it can also be controlled from a Bindable Event easily.


How you can implement this

So, you could do exactly what the example does but have it run through a BindableEvent (if your script is a typical Scripts) or a RemoteEvent (if your script is a Local Script). However, you could also do what @Jackscarlett said and make the Part nil. I’d recommend using my method because then you can do this for multiple things within one script through a RemoteEvent/BindableEvent and if the Main script ever breaks, you can go into just one script and fix it that way.


script.Parent:Destroy()

I believe it’s important to mention that when you destroy the part, its children (including the script) get removed as well. That means, no matter what you put after the wait() will not happen.

This is why you should find another way. @Jackscarlett has an interesting solution, but instead of doing Part, do Part.Parent.

The issue with this is, what if the Parent is one of the services such as workspace, serverstorage and so on… it’ll crash. I thought of that as well, lol.

Depending on how the script is handled, Part.Parent may refer to the workspace Service

Just setting the Part to nil alone would be fine since it’s only being referred to the BasePart

(Also I just chose a simple approach to make a part respawn, if you want it to make it more reliable and organized you can refer to what @MillerrIAm stated)

2 Likes