Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

4 comments

1

The Parent of the script gets destroyed before the script has waited 3 seconds

0

^ destroying the script's parent will destroy it's descendants as well

0

you forgot to set the clone's parent

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

1 answer

✓ Accepted answer
-- 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

2 comments

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?

0

Thanks,It works.But what does part.Parent mean?

This discussion is preserved as it was. New answers, comments, and votes are closed.