Help with Understanding NPC Chase Script

local Players = game:GetService("Players")

local function findNearestPlayer()
	
	local distance = 55
	local target = nil
	
	for i, v in pairs(Players:GetPlayers()) do
		local char = v.Character or v.CharacterAdded:Wait()
		local hum = char:FindFirstChildOfClass("Humanoid")
		local hrp = char:FindFirstChild("HumanoidRootPart")
		
		if char ~= nil and hum then
			if hum.Health> 0 then
				if (script.Parent.PrimaryPart.Position - hrp.Position).Magnitude < distance then
					target = hrp
					distance = (script.Parent.PrimaryPart.Position - hrp.Position).Magnitude
				end
			end

		end
	end
			
	return target
end

while true do
	task.wait(0.2)
	
	local target = findNearestPlayer()
	
	if target ~= nil then
		script.Parent:FindFirstChild("Humanoid"):MoveTo(target.Position)
	else
		task.wait(1)
		script.Parent:FindFirstChild("Humanoid"):MoveTo(script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-10,10), math.random(-10,10), math.random(-10,10)))
	end
	
end
2 Likes

I don’t understand this line of code.

Like why setting the distance equals to the distance difference, wouldn’t it reset once the while loop ends?

The distance variable is used to detect the closest player, I’ll explain:
This block of code right here, loops through all the players, and gets their characters.

It then does checks to see if the player is alive or not,

And then checks if the closest player, is further away from the NPC than this player. Using the distance variable as some sort of record.

This line just sets the distance variable, to how far it was away from the NPC. The variable is basically used as some sort of record. A record for the closest player distance to the NPC.

Since these lines of code are looped, the distance will keep adjusting to the closest distance, until there are no more players. Thus, giving the closest player to the npc.

TLDR; The distance variable is used to keep track of the current closest distance to the NPC, so that when it loops through all the players, the script can check if the player’s distance to the NPC, is closer than the past closest distance.


Yes it would, it’s used within the while loop to keep track of the closest distance to the player.

Wait, I thought like when the distance variable resets, it has a new value. It won’t have the old value? Like then how can the script checks if the distance is closer than the past distance? Like this part makes me think that line of code is pointless, but when testing it, it does have an impact. Just not sure how.

Basically, if the distance between the NPC and Player, is less than the distance variable, the distance variable gets set to the distance between the NPC and Player. (And the target is set to the player)

It will go through all the players, and the target and distance will keep changing to the closest distance/player.

Once the loop is done, the target variable will be set the the closest player, and then returned to whatever script that called the function. (The distance variable is discarded/unused)

Then how is setting distance variable useful? like it is in the end of the code and is not used anymore.

The distance variable is used to find the closest player. So as it goes through the players in game it finds there distance away from the part. If a players distance is < the current targets distance then it replaces the targets with the closer player. Once it loops through all the players it send the closest target back and uses the move to. After that it runs again to make sure it going to the closes played again.

1 Like

Sorry for the late response, had to go somewhere.

Think of it like like a bid.

They have to keep track of the the highest offering, and the person who offered the highest offering.

So then they have two types of data:
The Highest Offering (or the distance variable)
The Person who is the one with the Highest Offering (or the target variable)

They set the Highest Offering to the start price, so that nobody can bid lower than that.
Kind of like how the distance variable is set to 55, so that no player farther than that can be accounted for

Whoever offers more than the Highest Offering, is now the person who bid the most.
So, you set the Highest Offering (distance) to however much they bid. (how far they are from the npc)
You also set the Person who bid the most, to that person.

The loop works kind of like a reverse bid. (Except that it’s whoever has a closer distance)

  1. distance is set to 55, so that players that are farther away than that cannot be accounted for.

  2. The for loop goes through all the players in the game

  3. The loop checks if the players distance from the NPC is less than distance (check if they are offering more than the highest bid/if they are closer than the past closest distance)

  4. If it is, set distance to the players distance from the NPC (set the highest bid to how much they offered/how far away they are to the NPC)

  5. Also set the target to the player (set the current bid winner to the player)

  6. Repeat step 4 and 5 for each player (because it’s a loop)

  7. You will end up with the closest player (target variable/highest bidder)
    and how far away they are to the NPC (distance variable/bid amount)

The function will then be able to return the closest player. (highest bidder)

But wait? What about the distance variable/bid amount?:
Let’s just imagine that this bid, does not charge the people. If you’re not gonna charge them, you can just discard it.

1 Like

Oh so like if I don’t set the distance to the closest distance difference. Say if like a player’s distance to an npc is less than 55 and then another player is too, but much closer, the npc will keep chasing the first player right? Like ignores the second player. Like that’s the reason why we keep on setting the distance lower and lower.

And like if we did not do this in a function, is it possible? But certainly it’s gonna make things more complicated and confusing.

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