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

How to make a object regenerate when destroyed with a click detector?

Asked by 6 years ago

--When a object is destroyed it will wait 2 or more seconds and regenerate in the same spot. I've tried cloning the object but it wont work is anything wrong with my script? If there is please tell me!


if script.Parent.ClickDetector.MouseClick:connect(function() script.Parent:Destroy() end) then script.Parent.Clone() end

2 answers

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You have to parent the clone to workspace after cloning the part. Also, use : rather than . when calling functions such as Clone() or Destroy()

Here's what it should look like:

local thing = script.Parent
script.Parent = nil

local copy = thing:Clone()

local Reconnect
Reconnect = function()
    thing.ClickDetector.MouseClick:Connect(function()
        thing:Destroy()
        wait(2)
        thing = copy:Clone()
        thing.Parent = workspace
        Reconnect() --recursion
    end)
end

Reconnect()
Ad
Log in to vote
0
Answered by 6 years ago

put the part that you want to be respawned into replicated storage in the position you want it to be respawned in (if you want it to appear in different places move its position using PrimaryPart, which will have to be set..)

then use this script inside the respawn button which also has a click detector:

local respawn_button = script.Parent.ClickDetector

local function respawn()
    local original = game.workspace:FindFirstChild("model name here")
    original:Destroy() 
    local model = game.ReplicatedStorage:FindFirstChild("model name here"):Clone()
    model.Parent = game.workspace
end

respawn_button.MouseButton1Click:connect(respawn)

respawn()

Think this should work but I've just typed it here and didnt test it in studio :P

Answer this question