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

Object doesn't Clone when it gets Destroyed?Is there something wrong?

Asked by 5 years ago
Edited 5 years ago

The script below is for a EXP Orb so when someone collects it,it should be Destroyed and after awhile,It would be Cloned and pop back again but I don't see it.

They said there is something on line 15 but I don't know whats wrong,the Output doesn't tell anything. Can anyone help?

local db = true
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        if db == true then
            db = false
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            player.leaderstats.XP.Value = player.leaderstats.XP.Value + 1
            wait()
            script.Parent:Destroy()
        end
    end 
end)

while true do

    if db == false then
    db = true
    wait(3)
    script.Parent:Clone()   
end
end
1
The Parent of the script gets destroyed before the script has waited 3 seconds Spjureeedd 385 — 5y
0
^ destroying the script's parent will destroy it's descendants as well hellmatic 1523 — 5y
0
you forgot to set the clone's parent hellmatic 1523 — 5y
0
You would be better off with a script placed somewhere else. You can also do the clone in the touched event so you are not wasting processing power in a pointless while loop User#21908 42 — 5y

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago
Edited 5 years ago
-- instead of repeating 'script.Parent' use a variable to make your script more efficient
local Orb = script.Parent
local CanCollect = true 

local OrbClone = Orb:Clone() -- clone the orb before it gets destroyed

Orb.Touched:Connect(function(part) -- 'connect' is deprecated, use 'Connect'
    local parentModel = part.Parent
    local player = game.Players:GetPlayerFromCharacter(parentModel)
    if player and CanCollect then 
        CanCollect = false 
        player.leaderstats.XP.Value = player.leaderstats.XP.Value + 1
        script.Parent = game.ServerScriptService -- reparent the script so it doesn't get removed after Orb is destroyed
        Orb:Destroy()
        wait(5) -- orb respawn cooldown
        if OrbClone then 
            OrbClone.Parent = workspace
        end
        script:Destroy()
    end
end)

-- the while loop is unnecessary, just have the touched function handle the orb respawning
--while true do
--
--    if db == false then
--    db = true
--    wait(3)
--    script.Parent:Clone()   
--end
--end
0
So basically the Orb will get Cloned after getting destroyed but the script will get destroyed but not Cloned because the Orb already has the script right? rochel89 42 — 5y
0
Thanks,It works.But what does part.Parent mean? rochel89 42 — 5y
Ad

Answer this question