Clone script working differently on Studio and live game

I have a script that is failing to work and works differently in Roblox Studio and Roblox.
Output:

workspace.Cocoa Tree.Cocoa.Script:6: attempt to index nil with ‘Clone’
Stack Begin
Script ‘workspace.Cocoa Tree.Cocoa.Script’, Line 6
Stack End

My script:
local tree = script.Parent.Parent

local function RecieveCocoa(hit)
	local thingHit = hit.Parent
	if thingHit:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(thingHit)
		for i, v in pairs(plr.leaderstats:GetChildren()) do
			if v:IsA("IntValue") then
				local stat = v
				stat.Value = stat.Value + 1
			else
				error("Couldnt find stat")
			end
		end
	end
end

while true do
	wait(math.random(30,70))
	local newCocoa = script.Parent:Clone()
	newCocoa.Parent = nil
	script.Parent.Anchored = false
	wait(math.random(35,80))
	newCocoa.Parent = tree
	script:Destroy()
end

script.Parent.Touched:Connect()

What is wrong with the script? Thank you for taking your time.

I believe the problem is that you’re setting the parent to nil before setting it to tree. You don’t need to parent it to nil because it won’t show up in anything until you parent it to a service or object.

newCocoa.Parent = nil

pretty sure if you parent something to nil it removes it.

Try to remove that line and see if it fixes the problem.

2 Likes

Which line’s line 6? Looking at the code, the Clone’s after line 6. Is this the full script? If not, could you please provide it?

Pretty sure that’s your answer ^^

I know it’s way below 6 lines but it makes the most sense.

I don’t think that’s the line, as that’s setting the Parent property to tree. If the issue was parenting, it would throw an exception along the lines of attempt to index "Parent" with 'nil'. Setting the Parent property to nil wouldn’t make a different either, as a Cloned object’s parent’s set to nil after. The issue, according to the error, is that the script’s trying call Clone on nil.

@IAmPinleon I made a couple of changes to your script. I connected your connect to your function, and put the newCocoa variable after the tree declaration. I also declared a new variable called cocoa for the current cocoa bean.

local cocoa = script.Parent
local tree = cocoa.Parent
local newCocoa = cocoa:Clone()

local function RecieveCocoa(hit)
	local thingHit = hit.Parent
	if thingHit:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(thingHit)
		for i, v in pairs(plr.leaderstats:GetChildren()) do
			if v:IsA("IntValue") then
				local stat = v
				stat.Value = stat.Value + 1
			else
				error("Couldnt find stat")
			end
		end
	end
end

cocoa.Touched:Connect(RecieveCocoa)

while true do
	wait(math.random(30,70))
	cocoa.Anchored = false
	wait(math.random(35,80))
	newCocoa.Parent = tree
	cocoa:Destroy()
end

Try seeing what happens from there. My theory’s that something happens to the original cocoa before it goes to clone later.

1 Like

This helped me out a lot. Now I’ve been testing it and the RecieveCocoa() function hasn’t been working…

1 Like

That could be because of error, because it stops the execution of the thread. Removing that might fix that. Here’s a code example showing this.

print("I execute!"); -- I execute!
error("I errored!", 2); -- I errored!
print("I also executed!"); -- [No Output]

here is my code:
function RecieveCocoa(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local leaderstats = plr.leaderstats
if leaderstats ~= nil then
local stat = leaderstats:FindFirstChild(“Cocoa”)
if stat ~= nil then
stat.Value = stat.Value + 1
hit:Destroy()
else
error(“ERROR: Stat could not be found”)
end
end
end
end