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.
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.
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. )
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