Dummy not moving to the closest tree

I have a script here that simply moves a dummy to a tree, moves the tree onto the dummy and then the dummy drops it off.

My problem is trying to get the dummy to go to the closest tree, for some reason it is not working.

local dummy = script.Parent
local humanoid = dummy:FindFirstChildOfClass("Humanoid")
local trees = game.Workspace.Trees
local homebase = game.Workspace.HomeBase

local hasTree = false

local function moveToPosition(position)
	humanoid:MoveTo(position)
	humanoid.MoveToFinished:Wait()
end

local function collectTree(tree)
	if not hasTree and tree then
		print("Moving to tree")
		moveToPosition(tree.Position)
		tree.Anchored = false
		
		local vec = Vector3.new(dummy["Right Arm"].Position.X, dummy["Right Arm"].Position.Y + 1, dummy["Right Arm"].Position.Z)
		
		tree.Position = vec
		tree.Orientation = Vector3.new(0, 0, 90)
		
		local weld = Instance.new("WeldConstraint")
		weld.Name = "TreeWeld"
		weld.Part0 = tree
		weld.Part1 = dummy:FindFirstChild("Right Arm")
		weld.Parent = dummy

		print("Tree collected")
		hasTree = true
		tree.Parent = dummy
		
	end
end

local function returnToHomebase()
	if hasTree then
		print("Returning home with tree")
		humanoid:MoveTo(homebase.Position + Vector3.new(0, 3, 0))

		print("Dropping off tree at homebase")
		
		humanoid.MoveToFinished:Wait()
		
		if dummy:FindFirstChild("Tree") then
		
		dummy:FindFirstChild("Tree").Parent = game.Workspace.CollectedTrees
			
		else
			
			warn("Yeilding for Tree Find")
			
		end
		
		
		
		local weld = dummy:FindFirstChild("TreeWeld")
		if weld then
			weld:Destroy() 
		end
		
		hasTree = false
		
	end
end

while true do
	wait(0.25)
	
	local MaxDist = math.huge
	local ClosestPart
	local DummyHRP = dummy.HumanoidRootPart

	for _,v in pairs(trees:GetChildren()) do
		if v:IsA("BasePart") then
			local Dist = (DummyHRP.Position - v.Position).magnitude
			--warn(Dist)

			if Dist < MaxDist then
				MaxDist = Dist
				ClosestPart = v


	if not hasTree then
					if v then
						print(math.round(Dist))
			if v.Parent.Name == "Trees" and v.Parent == game.Workspace.Trees then
			collectTree(v)
			v.Parent = game.Workspace.GettingCollected
		else
			print("No tree found")
			end
		end
	else
		returnToHomebase()
				end
			end
		end
	end
end
  • Make a sanity check for HumanoidRootPart.
  • Make sure trees are accessible by the script.
  • Also, I think the main while loop should handle tree collection and returning to the home base separately.
  • I noticed that when a tree is collected, its parent changes, so you should make sure that the tree finding logic correctly handles trees moving between different parents.

Let me know if this helps

I am pretty amateur at scripting, what does this mean?

Also dont I already have this? they are both in two different functions.

You can add a sanity check like this:

local DummyHRP = dummy:FindFirstChild("HumanoidRootPart")
if not DummyHRP then
    warn("HumanoidRootPart not found")
end

To separate these concerns, you can structure the loop to check and handle each state individually:

while true do
    wait(0.25)
    
    if not hasTree then
        -- code for finding the nearest tree and collecting it
        local MaxDist = math.huge
        local ClosestPart
        local DummyHRP = dummy:FindFirstChild("HumanoidRootPart")
        if not DummyHRP then
            warn("HumanoidRootPart not found")
            return
        end

        for _, v in pairs(trees:GetChildren()) do
            if v:IsA("BasePart") then
                local Dist = (DummyHRP.Position - v.Position).magnitude

                if Dist < MaxDist then
                    MaxDist = Dist
                    ClosestPart = v
                end
            end
        end

        if ClosestPart then
            collectTree(ClosestPart)
            ClosestPart.Parent = game.Workspace.GettingCollected
        else
            print("No tree found")
        end
    else
        -- function for returning to the home base if a tree has been collected
        returnToHomebase()
    end
end

This way, the script first checks if hasTree is false to determine if the character needs to find and collect a tree. If hasTree is true, it proceeds with the logic for returning the tree to the home base.

Thanks so much this makes a lot more sense and works perfect!
(Sorry for the very late response, I have been on vacation :slight_smile: )

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