I need to achieve the NPC to recognize the soil plot after it has been spawned in.
Video 1 - Plots are spawned first, :GetChildren() is recognized by Farmer, he harvests.
Video 2 - Farmer is spawned first, :GetChildren() (presumably) is not recognized by Farmer, Farmer is not sure that there are plots to be harvested.
The Farmer will only recognize the plots before he is spawned in.
I have tried to add the same variables throughout the script to then recognize the plots even though he is spawned beforehand. Other ideas to solve the issue have abrupted, although that would include more work than necessarily needed. (The issue being he won’t recognize the plots if he is spawned before).
The Farmer will recognize the Soil Plot from :GetChildren().
If the Soil plot is spawned before the farmer, he will harvest. Not vise-versa.
-- Variables
local humanoid = script.Parent.Humanoid
local soil = game.Workspace.Soils:GetChildren()
local rest = game.Workspace.RestingPosition
local function checkGrownPlots()
for _, G in ipairs(soil) do
if G.GrownValue.Value == true then
print("Moving to a grown plot:", G.Name) --also use prints to help debugging
humanoid:MoveTo(G.Position)
return true
end
end
return false
end
-- Script Logic
for _, G in ipairs(soil) do
G.GrownValue:GetPropertyChangedSignal("Value"):Connect(function()
if not checkGrownPlots() then --uses the returned value
print("Moving to resting position")
humanoid:MoveTo(rest.Position)
end
end)
end
Spawn Script:
-- Variables
local clickDet = script.Parent.ClickDetector
local structure = game.Workspace.Soils
local tween = game:GetService("TweenService")
local sound = game.ReplicatedStorage.Sounds.Pop2
-- Functions
-- Script Logic
clickDet.MouseClick:Connect(function()
sound:Play()
local structCopy = structure.Soil:Clone()
structCopy.Parent = game.Workspace.Soils
structCopy.Position = script.Parent.Pos.Position
task.wait(.2)
script.Parent:Destroy()
end)