Attempt to index nil with number

I’m trying to create a AI for Baldi but I keep on getting attempt to index nil with number for some reason.

--Main AI for baldi

--//Settings
local Range = 50000

--//Variables
local Players = game:GetService("Players")

local HRP = script.Parent:WaitForChild("HumanoidRootPart") or script.Parent:WaitForChild("Torso")

--//Stuff

local function GetTheClosestNumber(tab)
	local tier = 0 --The smallest number to return
	local lastValue --The value before current value
	
	for index, value in pairs(tab) do --Loop the table
		--print(index.." looped") --Print the index like this " "index.." looped" "
		value = tonumber(value) --Make the value to a number just in case
		lastValue = tab[index - 1] or -50 --The index before the current index or the last value
		
		if value[2] < lastValue then --Compare the value
			--If it's smaller than the last value overwrite tier with the value variable
			tier = value[2]
		end --End wrap
	end --End wrap
	
	return tier --Return the tier which has smallest number
end


local function GetNearPlayers()
	--Get the closest player near instead of player's near by the range distance
	local Target = nil --Target value which is to be returned
	local Tab = {} --Table to store information about the player's
	local SmallValue = 0 --Smallest distance value using GetTheClosestNumber
	local DistanceValue = 0 --Current distance for that player
	
	for index, Model in pairs(workspace:GetChildren()) do --Loop in workspace
		if Players:GetPlayerFromCharacter(Model) then --Check if it return's something
			DistanceValue = (Model.HumanoidRootPart.Position or Model.Torso.Position - HRP.Position).Magnitude --Distance between the player and Baldi
			Tab[index] = {Model, DistanceValue} --Store it in the table
		end --End wrap
	end --End wrap
	
	SmallValue = GetTheClosestNumber(Tab) --Use GetTheClosestNumber to get the closest distance
	
	for index, tabLe in pairs(Tab) do --Loop in the table
		if tabLe[2] == SmallValue then --Check if the distance is equal to SmallValue
			Target = tabLe[1] --Set the Target variable to the player's model
		end --End wrap
	end --End wrap
	
	print(Target)
	
	return Target --Return the Target variable
end


while true do
	task.wait(2)
	print(GetNearPlayers().Name or "no target")
end

Can you show where exactly the error is located?

20:41:25.992 Workspace.Baldi.AI:22: attempt to index nil with number - Studio 20:41:25.992 Stack Begin - Studio 20:41:25.992 Script 'Workspace.Baldi.AI', Line 22 - function GetTheClosestNumber - Studio 20:41:25.992 Script 'Workspace.Baldi.AI', Line 46 - function GetNearPlayers - Studio 20:41:25.992 Script 'Workspace.Baldi.AI', Line 62 - Studio 20:41:25.992 Stack End

Not the Error, the Line

text

fggfytdfytrftyrtyr

bruh,

Im talking about which line on the script

It’s if value[2] < lastValue then. My bad.

Couldn’t you do this?

for index, tabLe in pairs(Tab) do --Loop in the table
		if tabLe[index] == SmallValue then --Check if the distance is equal to SmallValue
			Target = tabLe[index] --Set the Target variable to the player's model
		end --End wrap
	end --End wrap

No, tabLe[2] is the Magnitude.

Try doing this:

for index, Model in pairs(workspace:GetChildren()) do --Loop in workspace
		if Players:GetPlayerFromCharacter(Model) then --Check if it return's something
			DistanceValue = (Model.HumanoidRootPart.Position or Model.Torso.Position - HRP.Position).Magnitude --Distance between the player and Baldi
     table.insert(Tab,Model); table.insert(Tab, DistanceValue)
		end --End wrap
	end --End wrap

Now I get 20:54:25.475 Workspace.Baldi.AI:75: attempt to index nil with 'Name' - Studio 20:54:25.475 Stack Begin - Studio 20:54:25.475 Script 'Workspace.Baldi.AI', Line 75 - Studio 20:54:25.475 Stack End

Please make sure you show what line it is on inside the script

It’s this part print(GetNearPlayers().Name or “no target”).

Try removing the Name part :confused:

Now it print’s no target.

grfdgfdgfgfdhgfd

Maybe try this?

	return Tab[1]
end


while true do
	task.wait(2)
	print(GetNearPlayers().Name or "no target")
end

Same thing. :confused: dfasdasasdda trio! trio!

Just always assume that it could return nil | Player. Check if it’s there and print the name. Otherwise just do nothing and say “no target”.

while true do
	task.wait(2)
    local target = GetNearPlayers()
    if target then
	    print(target.Name)
    else
        print("no target")
    end
end

Again. It’s the same thing as GetNearPlayers().Name or "no target".

If you read the error message you’ll see that your problem is in line 22 of your script.

You are trying to index a number, in your case value which won’t work.

Consider this:

local value = 123
print(value[2])

What do you expect the output to be?

Also, converting a value with tonumber() will return nil if the value is not like a number, for example an alphabetic string.

local a = "hello"
print(tonumber(a))

You’re better off handling this when you populate the table with players in GetNearPlayers().