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

Attempt to index nil with "Clone" error?

Asked by 4 years ago

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)

0
You can't clone a model after destroying it. kingblaze_1000 359 — 4y
0
^ this. Also you're using a couple deprecated terms like "findFirstChild" and "connect." Use "FindFirstChild" and "Connect" instead. lunatic5 409 — 4y
0
Thank you! This helped. CaptainStevones123 12 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

Ad

Answer this question