Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

For some reason the dummy is not walking towards the player? No error in output??

local players = game:GetService("Players"):GetChildren()
local target
local nearest = {}
local movemessage = "The parent humaniod has tried to move, but unfortunately might've failed."

while wait() do
for i = 1,#players do
	local distance = (script.Parent.Torso.Position-players[i].Character.Torso.Position).magnitude
	if distance <= 20 then
		table.insert(nearest,1,players[i].Character.Torso.Position)
		table.sort(nearest,#nearest)
		target = nearest[1]
	else
			return
	end
end
end

local debounce = false

while wait(2) do
	walktoplayer()
end

function walktoplayer()
	if target ~= nil and debounce == false then
		debounce = true
		local path = game:GetService("PathfindingService"):FindPathAsync(script.Parent.Torso, target)
		local points = path:GetWaypoints()
		
		for i,v in pairs(points) do
			local stud = Instance.new("Part", workspace)
			stud.Anchored = true
			stud.CanCollide = false
			stud.CFrame = CFrame.new(v.Position)
			stud.Size = Vector3.new(1,1,1)
		end
		
		if path.Status == Enum.PathStatus.Success then
			for i,v in pairs(points) do
				script.Parent.Humanoid:MoveTo(v.Position)
				print(movemessage)
				script.Parent.Humanoid.MoveToFinished:Wait()
				if v.Action == Enum.PathWaypointAction.Jump then
					script.Parent.Humanoid.Jump = true
				end
			end
		end
		debounce = false
	end
end

Doesn't even print the movemessage. What's the issue?

1 comment

0

Maybe try making the NPC to go to the HumanoidRootPart, not the Torso.

1 answer

local players = game:GetService("Players"):GetChildren()
local target
local nearest = {}
local movemessage = "The parent humaniod has tried to move, but unfortunately might've failed."

while wait() do
for i = 1,#players do
    local distance = (script.Parent.Torso.Position-players[i].Character.Torso.Position).magnitude
    if distance <= 20 then
        table.insert(nearest,1,players[i].Character.Torso.Position)
        table.sort(nearest,#nearest)
        target = nearest[1]
    else
            return
    end
end
end

local debounce = false

while wait(2) do
    walktoplayer()
end

function walktoplayer()
    if target ~= nil and debounce == false then
        debounce = true
        local path = game:GetService("PathfindingService"):FindPathAsync(script.Parent.Torso, target)
        local points = path:GetWaypoints()
	local ExistedPoints = {}
	local index= 0
        for i,v in pairs(points) do
            local stud = Instance.new("Part", workspace)
	    stud.Name = "Waypoint[" .. i .. "]"
            stud.Anchored = true
            stud.CanCollide = false
            stud.CFrame = CFrame.new(v.Position)
            stud.Size = Vector3.new(1,1,1)
	    index = index + 1
	    ExistedPoints[index] = stud
        end

        if path.Status == Enum.PathStatus.Success then
            for i,v in pairs(ExistedPoints) do
                script.Parent.Humanoid:MoveTo(v.Position)
                print(movemessage)
                script.Parent.Humanoid.MoveToFinished:Wait()
                if points[i].Action == Enum.PathWaypointAction.Jump then
                    script.Parent.Humanoid.Jump = true
                end
            end
        end
        debounce = false
    end
end

The points are not real, make them real and establish a table for them.

This discussion is preserved as it was. New answers, comments, and votes are closed.