I’m trying to figure out how to make a model re-spawn if it gets destroyed. The basic set up is as such.
Under Workspace is a spawn folder containing multiple blocks that act as spawn locations for an animal Model to appear at. Each spawn location will receive a cloned animal.
Under ReplicatedStorage is a Folder containing the animal Model
In ServiceScriptService is a script that clones the animal Model from RepStor to each of the spawn points at the start of the game.
During game play animal Models may get pushed off the edge of the world and are naturally destroyed, never to return because I don’t know how to clone a Model if destroyed. I suppose it’s the code/setup to recognize if a model is destroyed that I don’t know. After … Model:Clone() back to the same spawn point.
I believe you are looking for childRemoved, check if the child removed is an animal, clone the right type of animal, set the CFrame of it to a spawn location, and parent it to the workspace.
You could just put the model to server storage by script (1), or clone the original model before destroying it (2), therefore (1) you could put it back again with GetService function (ServerStorage) by changing its parent again, or (2) get the clone version.
You can try using .AncestryChanged to detect when the animal is destroyed, and then clone in a new one. Also: if the client doesn’t need to directly interact with an instance, then it should be placed in ServerStorage instead to reduce the amount of instances that need to be replicated.
local function spawnAnimal()
--[[ code to spawn in the animal goes up here ]]--
-- animal should be the model that gets cloned, change to whatever variable you were using before
animal.AncestryChanged:Connect(function(_, parent)
if parent == nil then -- the animal has fallen into the void
spawnAnimal() -- spawn in a new animal
end
-- dont worry about a memory leak occuring here,
-- :Destroy()'ed instances get all their connections autimatically disconnected
end
-- call the function once to initially spawn in the animal
spawnAnimal()