Trying to make a npc follow the player with the most damage dealt

The name of the topic suggests it: I am trying to make a NPC follow a player with the most damage dealt, but for some reason I’m getting the error,

‘Workspace.Thug.NPCScript:233: attempt to index nil with ‘Name’’.

(for context, I make a folder in the NPC called Attackers and put a NumberValue in it with their corresponding Player’s name, for each Player in the game.)

This method works just fine when finding the NPC’s killer, but doesn’t work here.

	local attacker_list = Attackers:GetChildren()
	local sort_function = function(attacker1, attacker2)
		return attacker1.Value > attacker2.Value;
	table.sort(attacker_list, sort_function)
	local bestAttacker = attacker_list[1]
	local character = workspace:FindFirstChild(bestAttacker.Name)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(character)
	if Player then
		if bestAttacker.Value > 0 then
			-- do stuff
1 Like

It is telling you that it can’t find an object with the name assigned to bestAttacker. Try printing the bestAttacker value after you set it to see what it is after the table sort.

By what you’ve given it appears as though best attacker is nil meaning the table: attacker_list is most likely also nil or contains only nil values. Debug and report with these:


It works great now, thanks guys!

1 Like