NPC Won’t recognize cloned waypoint

I need to achieve the NPC to recognize the soil plot after it has been spawned in.

image

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)

From what you have shown, the ‘soil’ variable is only defined once at the top of the script, therefore, in the second video, the farmer fetches an empty list and has no way of knowing when a new plot has been added. You should utilize the ‘ChildAdded’ event to overwrite the value of ‘soil’ every time a new plot has been added to ‘Soils’.

Here’s a simple example:

game.Workspace.Soils.ChildAdded:Connect(function()
	soil = game.Workspace.Soils:GetChildren()
end)

Thank you so much friend, this has helped out incredibly. Appreciate you much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.