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
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
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
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