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

Attempt to index nil with "Clone" error?

Hello, I'm having a problem cloning a model after destroying it, here is my code.

amount = 1 
banana = game.Workspace.Banana
banana.Archivable = true
function onTouched(Banana)
 local h = Banana.Parent:findFirstChild("Humanoid")
 if (h~=nil) then
  local thisplr = game.Players:findFirstChild(h.Parent.Name)
 if (thisplr~=nil) then
   local stats = thisplr:findFirstChild("leaderstats")
   if (stats~=nil) then
    local score = stats:findFirstChild("Bananas")
    if (score~=nil) then
    score.Value = score.Value + amount
script.Parent:Destroy()
wait(3)
script.Parent:Clone()
    end
   end
 end
  
 end
end

script.Parent.Touched:connect(onTouched)

3 comments

0

You can't clone a model after destroying it.

0

^ this. Also you're using a couple deprecated terms like "findFirstChild" and "connect." Use "FindFirstChild" and "Connect" instead.

0

Thank you! This helped.

1 answer

You Cannot clone anything after destroying it. If you have destroyed it, the script doesn't know what you're telling it to clone. if u want the clone to appear after you've destroyed the previous part then do this

amount = 1 
banana = game.Workspace.Banana
banana.Archivable = true
function onTouched(Banana)
 local h = Banana.Parent:FindFirstChild("Humanoid")
 if (h~=nil) then
  local thisplr = game.Players:FindFirstChild(h.Parent.Name)
 if (thisplr~=nil) then
   local stats = thisplr:FindFirstChild("leaderstats")
   if (stats~=nil) then
    local score = stats:FindFirstChild("Bananas")
    if (score~=nil) then
    score.Value = score.Value + amount
local cloned = script.Parent:Clone()
script.Parent = cloned
script.Parent:Destroy()
wait(3)
cloned.Parent = game.Workspace --//This is if it is a server script. If it is a local script then replace this line with **cloned.Parent = workspace**
    end
   end
 end

 end
end

script.Parent.Touched:Connect(onTouched)

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