Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to clone models on getting destroyed and respawn them?

Asked by 5 years ago

Imagine there is a car,when it touches lava or falls from sky then how to respawn it to it's starting position..I know how to clone a model but don't know how to clone it to on its first point when it touches lava or falls from sky

2 answers

Log in to vote
0
Answered by
SCP774 191
5 years ago

Well, you should put the car instances in a specific model, (Like the model that the car's spawn is in) make a script that fires when a child is removed and make it check if the car model is still there each time a child is removed. If it's not there, it'll clone a car from anywhere you stored the car model in and then move it's PRIMARY PART's position to the spawn part's position. (You may need to weld everything in the car model.) Sorry but I'm too lazy to make a script about it.

Ad
Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
5 years ago

This should do it.

local car = workspace.Car --change this into the car model you're using.
local backup = car:Clone()

function onCarDestroy(car, parent, CF, override) --if parent or CF or override are nil, the script will still run
    local newCar
    if override then
        newCar = car:Copy()
    else
        newCar = backup:Clone()
    end
    car:Destroy()
    if parent then
        newCar.Parent = parent
    else
        newCar.Parent = workspace
    end
    if CF then
        if newCar.PrimaryPart then
            newCar:SetPrimaryPartCFrame(CF)
            return newCar
        else
            error(newCar.Name.." has no PrimaryPart, no position set.")
            return newCar
        end
    else
        return newCar
    end
end

--all you need to do now is call onCarDestroy(car) when you are destroying the car.
--[[
ARGUMENTS:
    **[object; car]** > The model that will get destroyed and a new copy will be made.
    **[object; parent]** > [OPTIONAL] The parent of the new copy.
    **[cframe; CF]** > [OPTIONAL] The cframe the new copy will be positioned and rotated at, (The car must have a primary part for this to work)
    **[bool; override]** > [OPTIONAL] If true, the function will clone the first argument instaid of the backup, if cf is nil and override is true, the new copy will spawn on the same spot at the old car before it was destroyed. 
--]]

I've also put a description of the arguments the function takes, if you needed that.

Answer this question