While loops not replicating across clones

I’m making a farming game this time. I’ve added carrot seeds and potato seeds, and they can be planted. You are also able to harvest the crops. Everything worked perfectly fine until today, when, even though I haven’t edited the script, every plant past the first patch you plant doesn’t grow anything.

This piece of code is inside the seeds tool…

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "CropPlot" then
		hit.Parent.Name = "PotatoPlot"
		local crop = game.ReplicatedStorage.PotatoPatch:Clone()
		crop.Parent = hit.Parent.Parent
		crop:SetPrimaryPartCFrame(hit.Parent.PrimaryPart.CFrame)
		script.Parent.Parent:Destroy()
	end
end)

And this one is inside the patch itself:

local growtime = 200
local plant = game.ReplicatedStorage.PotatoCrop

while true do
	wait()
	if script.Parent.Parent.Parent == game.Workspace then
		print("Growing crops...")
		game.Workspace.Rustle:Play()
		wait(5)
		local crops = plant:Clone()
		crops.Parent = game.Workspace
		crops:SetPrimaryPartCFrame(script.Parent.CFrame)
		game.Workspace.Pop:Play()
		wait(30)
		crops:Destroy()
	end
end

On planting one patch, it works perfectly. Any other patch I plant after that doesn’t even send the print(). Is there something missing?

Another thing: If I try to grow carrots after growing potatoes, or the other way around, even planting the second crop for the first time, it doesn’t grow.

What do you mean by “planting one patch” and also can we see what line 3 is checking?

(You should also use coroutine.)

Line 3 is checking whether or not the crops are in Workspace or not. If they’re not, they won’t grow stuff because that’s how it works.

When you plant a seed, it spawns a patch of the veggie of your choice. When I plant a patch for the first time, it works perfectly. The next time (and any time after that) I plant any patch of any variety, it never triggers.

Could you show me more of the script? Also i’m pretty sure its because you are doing:

crops:Destroy()

I could be wrong but comment out that line and see if anything changes.

There isn’t any more to the script. I’ll try out commenting it out though!

Nothing changed when commenting it out. No errors, no nothing in terms of the output window.

Can you show me the explorer for this line please? (Like just screenshot the script and the parent it is getting inside of explorer)

script.Parent.Parent.Parent == game.Workspace

Also when the loop ends and starts another one, can you tell me if everything is the same; like nothing got deleted/destroyed.

You got it!explorer The script inside PotatoSpawn is the script.
Again, it works perfectly for the first patch I plant, but any others don’t.

It looks like you put one too many parents in the if statement. What you are doing is seeing if replicatedstored is in workspace. Try script.Parent.Parent

(I have no clue why this would have looped the first time if that is the case. :slight_smile: )

No? I am checking if CarrotPatch is in Workspace. I have the correct amount of .Parents, and when I attempt to run the game, it doesn’t work at all.

I might actually try a workaround where the planting seeds turns on the script. It might work then.

The while true do might be going too fast.
You could try firing a _G function when everything loads and then it runs the while wait()/true do.

Your if statement about checking if the patch is under the workspace can be broken easily if the amount of parents are changed. You can easily fix this by using :IsDescendantOf(). Heres a fixed versioon of your patch script:

local growtime = 200
local plant = game.ReplicatedStorage.PotatoCrop

while true do
	wait()
	if script.Parent:IsDescendantOf(workspace) then -- Use :IsDescendantOf instead, which checks if the instance is a descendant of what is passed
		print("Growing crops...")
		game.Workspace.Rustle:Play()
		wait(5)
		local crops = plant:Clone()
		crops.Parent = game.Workspace
		crops:SetPrimaryPartCFrame(script.Parent.CFrame)
		game.Workspace.Pop:Play()
		wait(30)
		crops:Destroy()
	end
end

You can read more about :IsDescendantOf() below:

:IsDescendantOf()

I hope this helps!

Thank you! It worked perfectly!