Moving NPC need help

  1. What do you want to achieve?
    I want to learn how to make a moving NPC. All I need is what way do you move it and how do you move it to the players and not targeting one player.

  2. What is the issue?
    I can’t Learn how to make it instead the videos just give you the script.

  3. What solutions have you tried so far?
    I went to YouTube and looked at the DevForum

(message deleted. -------------------------)

I will try it thank you for this.

I meant is there a way I can understand the code?

There are plenty of posts about this on the DevForum, plus I don’t see why you can’t learn from a youtube videos. They give you the script? Look on the DevHub to see how the various parts of the script work. It’s really easy to script trust me.

Sorry, But what DevCramp gave me did not help.

I can’t learn from YouTube videos is because they just give the script in the desc and I don’t understand the code.

-- What does this even mean???
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(math.random(1,5))
	--local target = findNearestTorso(script.Parent.Torso.Position)
	--if target ~= nil then
	--	script.Parent.Zombie:MoveTo(target.Position, target)
	--end

	script.Parent.Humanoid:MoveTo(Vector3.new(math.random(-100,100),0,math.random(-100,100)), game.Workspace.Base)
	

end

Actually, this is a pretty basic script for a chasing NPC. It finds the closest target that contains a humanoid inside of it, and then uses Humanoid:MoveTo() to move the NPC to the found torso. I think this is a very old script, as it even uses :children() instead of :GetChildren() which i’ve never seen be used in any newer scripts. Divide the script step by step. For example, start from the beginning. It defines two variables, “leftarm” and “rightarm”. I think their names define them good enough. You don’t know what :FindFirstChild() does, then search on the DevHub.

Hey @DEVLocalPlayer can you help me make a game?

I want to be good so I want to partner with you if you want. I am not forcing anyone.

Anyway, thank you this will help a lot.

1 Like
local NPC = workspace.NPC -- Choose
local pathfinding_service = game:GetService("PathfindingService") -- Gets PathfindingService
 
function getClosestPlayer() -- Creates Function
        local closest_player, closest_distance = nil, 200 -- Makes some variables to use later
        for i, player in pairs(workspace:GetChildren()) do -- Gets everything in workspace
                if player:FindFirstChild("Humanoid") and player ~= NPC then -- Checks if is a player and is not npc
                        local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
                        if distance < closest_distance then -- Gets Closest Player From Magnitude
                                closest_player = player
                                closest_distance = distance
                        end
                end
        end
        return closest_player, closest_distance
end
 
while true do -- Loop
        local path = pathfinding_service:CreatePath() -- Creates a path, in PathfindingService
        local player, distance = getClosestPlayer() -- Calls the getClosestPlayer function
        if player and distance > 10 then 
                path:ComputeAsync(NPC.PrimaryPart.Position, player.PrimaryPart.Position) -- Asyncs the two positions
                local waypoints = path:GetWaypoints() -- Gets waypoints
                for _, waypoint in pairs(waypoints) do -- Goes over every waypoint
                        NPC.Humanoid:MoveTo(waypoint.Position) -- Makes npc walk to wayPoint
                        while true do
                                local distance = (NPC.PrimaryPart.Position - waypoint.Position).Magnitude
                                local distance_from_player = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
                                if distance < 5 then
                                        break
                                end
                                if distance_from_player < 10 then
                                        NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
                                        break
                                end
                                wait()
                        end
                end
        end
        wait()
end
1 Like

I can help you if you need help with explaining scripts, I’m already working on a couple projects so i’m not willing to partner with other people right now.

By the way, he said he needs an explanation and not an already made script. He could search up on YouTube or on the toolbox for an already made one.

Then can I help you for free ?
I know how to make the basics like functions clickdecoters and other things

can I help you for learning new things?

https://www.lua.org/pil/7.3.html