Bad argument #1 (number expected, got no value) with math.min

Here’s the code:

local humanoidrootpartpos = {}


function getHumanoid(model)
	for _, h in pairs(model:GetChildren()) do
		if h:IsA("Humanoid") then
			return h
		end
	end
end

local ai = script.Parent
local human = getHumanoid(ai)
local hroot = ai.HumanoidRootPart
local hspeed = hroot.Velocity.magnitude
local pfs = game:GetService("PathfindingService")

function GetPlayerNames()
	local players = game:GetService("Players"):GetChildren()
	local name = nil
	for _, p in pairs(players) do
		if p:IsA("Player") then
			name = tostring(p.Name)
		end
	end
	return name
end


function GetPlayersBodyParts(t)
	local torso = t
	if torso then
		local figure = torso.Parent
		for _, v in pairs(figure:GetChildren()) do
			if v:IsA("Part") then
				return v.Name
			end
		end
	else
		return ("HumanoidRootPart")
	end
end

function GetTorso(part)
	local chars = game.Workspace:GetChildren()
	local torso = nil
	for _, c in pairs(chars) do
		if c:IsA("Model") and c ~= script.Parent and c.Name == GetPlayerNames() then
			local charRoot = c:FindFirstChild("HumanoidRootPart")
			local mag = (charRoot.Position - part).magnitude
			table.insert(humanoidrootpartpos,mag)
			print(humanoidrootpartpos[1])
				torso = charRoot
		end
	end
	return torso
end

for _, aiparts in pairs(ai:GetChildren()) do
	if aiparts:IsA("Part") then
		aiparts.Touched:Connect(function(p)
			if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= ai.Name then
				local enemy = p.Parent
				local ehuman = getHumanoid(enemy)
ehuman.Health = 0
	
			end
		end)
	end
end

local path
local waypoint
local oldpoints
local isWandering = 0



while wait() do
local enemytorso = GetTorso(hroot.Position)
	local TargetMag = math.min(unpack(humanoidrootpartpos))

	if enemytorso ~= nil then -- if player detected
		if (enemytorso.Position - hroot.Position).magnitude == TargetMag then
		isWandering = 1
		local function checkw(t)
			local ci = 3
			if ci > #t then
				ci = 3
			end
			if t[ci] == nil and ci < #t then
				repeat
					ci = ci + 1
					wait()
				until t[ci] ~= nil
				return Vector3.new(1, 0, 0) + t[ci]
			else
				ci = 3
				return t[ci]
			end
		end
		
		path = pfs:FindPathAsync(hroot.Position, enemytorso.Position)
		waypoint = path:GetWaypoints()
		oldpoints = waypoint
		local connection;
		
		local direct = Vector3.FromNormalId(Enum.NormalId.Front)
		local ncf = hroot.CFrame * CFrame.new(direct)
		direct = ncf.p.unit
		local rootr = Ray.new(hroot.Position, direct)
		local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
		
		if path and waypoint or checkw(waypoint) then
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
				human:MoveTo( checkw(waypoint).Position )
				human.Jump = false
			end
			
		
			
			if connection then
				connection:Disconnect()
			end
			
		else
			for i = 3, #oldpoints do
				human:MoveTo( oldpoints[i].Position )	
			end
		end
end
end
end

I made a script for this ai detecting the closest player and make it move to it

it doesnt find the smallest magnitude and just errors when I use math.min
here’s the line that is errored and the output:

Line:

local TargetMag = math.min(unpack(humanoidrootpartpos))

Output: bad argument #1 (number expected, got no value)

(Reposted due to updates.)

Your error is because “humanoidrootpartpos” doesn’t have a number inside the table. Try printing the value of humanoidrootpartpos and see what you get, reply with what you get.

I tried the following

local humanoidrootpartpos = {}

local charRoot = game.Players.Aldanium.Character:FindFirstChild("HumanoidRootPart")
local mag = (charRoot.Position - workspace.lonelypart.Position).magnitude
table.insert(humanoidrootpartpos,mag)

local TargetMag = math.min(unpack(humanoidrootpartpos))

I got no error whatsoever. Try printing some values and you might be able to find the cause to your error.

I printed humanoidrootpartpos[1] and nothing printed

Then that means your table is empty. Weird…

Try this

local humanoidrootpartpos = {}


function getHumanoid(model)
	for _, h in pairs(model:GetChildren()) do
		if h:IsA("Humanoid") then
			return h
		end
	end
end

local ai = script.Parent
local human = getHumanoid(ai)
local hroot = ai.HumanoidRootPart
local hspeed = hroot.Velocity.magnitude
local pfs = game:GetService("PathfindingService")

function GetPlayerNames()
	local players = game:GetService("Players"):GetChildren()
	local name = nil
	for _, p in pairs(players) do
		if p:IsA("Player") then
			name = tostring(p.Name)
		end
	end
	return name
end


function GetPlayersBodyParts(t)
	local torso = t
	if torso then
		local figure = torso.Parent
		for _, v in pairs(figure:GetChildren()) do
			if v:IsA("Part") then
				return v.Name
			end
		end
	else
		return ("HumanoidRootPart")
	end
end

function GetTorso(part)
	local chars = game.Workspace:GetChildren()
	local torso = nil
	for _, c in pairs(chars) do
		if c:IsA("Model") and c ~= script.Parent and c.Name == GetPlayerNames() then
			local charRoot = c:FindFirstChild("HumanoidRootPart")
			local mag = (charRoot.Position - part).magnitude
			table.insert(humanoidrootpartpos,mag)
			print(humanoidrootpartpos[1])
				torso = charRoot
		end
	end
	return torso
end

for _, aiparts in pairs(ai:GetChildren()) do
	if aiparts:IsA("Part") then
		aiparts.Touched:Connect(function(p)
			if p.Parent.Name == GetPlayerNames() and p.Parent.Name ~= ai.Name then
				local enemy = p.Parent
				local ehuman = getHumanoid(enemy)
ehuman.Health = 0
	
			end
		end)
	end
end

local path
local waypoint
local oldpoints
local isWandering = 0



while wait() do
local enemytorso = GetTorso(hroot.Position)
	if enemytorso ~= nil then -- if player detected
                local TargetMag = math.min(unpack(humanoidrootpartpos))
		if (enemytorso.Position - hroot.Position).magnitude == TargetMag then
		isWandering = 1
		local function checkw(t)
			local ci = 3
			if ci > #t then
				ci = 3
			end
			if t[ci] == nil and ci < #t then
				repeat
					ci = ci + 1
					wait()
				until t[ci] ~= nil
				return Vector3.new(1, 0, 0) + t[ci]
			else
				ci = 3
				return t[ci]
			end
		end
		
		path = pfs:FindPathAsync(hroot.Position, enemytorso.Position)
		waypoint = path:GetWaypoints()
		oldpoints = waypoint
		local connection;
		
		local direct = Vector3.FromNormalId(Enum.NormalId.Front)
		local ncf = hroot.CFrame * CFrame.new(direct)
		direct = ncf.p.unit
		local rootr = Ray.new(hroot.Position, direct)
		local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
		
		if path and waypoint or checkw(waypoint) then
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
				human:MoveTo( checkw(waypoint).Position )
				human.Jump = false
			end
			
		
			
			if connection then
				connection:Disconnect()
			end
			
		else
			for i = 3, #oldpoints do
				human:MoveTo( oldpoints[i].Position )	
			end
		end
end
end
end

Let me know if it works.

It ony works once, and after that it will not follow me anymore, but it does kill me.

But it doesn’t error right?

( 30 chars )

It works when there is one player, but when there are 2+ it does what I stated above. No errros.

I haven’t really looked into the script as much as to come to a conclusion but you can try the following.

  • Instead of doing GetPlayerNames() as a function I think you’re better off using a for loop to check for players without a function.

  • Script might be just going through one character and then breaking/ending the loop. Try rescripting it in a neater way so you can figure the issue out.

edit: always try adding prints around the script to print how many times they run etc. It’ll be really helpful.

I found another problem, it kills only one player.

I tried debugging it, but everything prints the first time, and the second time it just doesnt work,

I found something, when printing the magnitude, it stays the same when I move. I’ll try adding the functions in the while wait() function