Attempt to index nil with number

I have a boss which attacks and addresses attacks on via his humanoid which is meant to aggro on the enemy/player, I have a script which locates the humanoid and different several models, locations and parts.

This is the script, the error is on line 7

if Dist < TargetInfo[2] then
local i__Boss__i = script.Parent.Boss
local i__Players__i = game.Players:GetPlayers()
local i__TargetInfo__i = {nil,200}

for i = 1,#i__Players__i do
	local Dist = (i__Players__i[i].Character:WaitForChild("HumanoidRootPart").Position - i__Boss__i.Parent:WaitForChild("HumanoidRootPart").Position).magnitude
if Dist < TargetInfo[2] then
	TargetInfo = {i__Players__i[i],Dist}

local i__GasterTest__i = game:GetService('ServerStorage'):WaitForChild('GasterTest')
local i__GasterTestMulti1__i = game:GetService('ServerStorage'):WaitForChild('GasterTestMulti'):WaitForChild("GasterTest1")
local i__GasterTestMulti2__i = game:GetService('ServerStorage'):WaitForChild('GasterTestMulti'):WaitForChild("GasterTest2")
local i__GasterTestMulti3__i = game:GetService('ServerStorage'):WaitForChild('GasterTestMulti'):WaitForChild("GasterTest3")
local i__BoneZone__i = game:GetService('ServerStorage'):WaitForChild('SansBonezone')
local i__Bones__i = game:GetService('ServerStorage'):WaitForChild("Bones")
local i__humanoidrootpart__i = script.Parent.HumanoidRootPart
local i__playerhumanoidrootpart__i = TargetInfo[1].Character:WaitForChild("HumanoidRootPart")

local v1 = nil
local v2 = nil
local v3 = nil
local v4 = nil
local v5 = nil
local v6 = nil
local v7 = nil
local v8 = false
local v9 = false
local v10 = false
local v11 = false

Here is a screenshot of the boss’s descendants/children :

1 Like

Error is saying TargetInfo is nil. I don’t see where it is defined, could you point to it.

1 Like

Tip:
Instead of all these long lines,
Simply define local ServerStorage = game:GetService("ServerStorage") , and access whatever you need from there.
In this case, you could do:

local ServerStorage = game:GetService("ServerStorage")
local GasterTestMulti = ServerStorage:WaitForChild("GasterTestMulti")
local GasterTest1,GasterTest2,GasterTest3 = GasterTestMulti:WaitForChild("GasterTest1"),GasterTestMulti:WaitForChild("GasterTest2"),GasterTestMulti:WaitForChild("GasterTest3")
local SansBonezone = ServerStorage:WaitForChild("SansBonezone")
local Bones = ServerStorage:WaitForChild("Bones")

Lets implement this into your code, so it’s neater to work with:

local Boss = script.Parent.Boss
local Players = game:GetService("Players"):GetPlayers() 
local TargetInfo = {nil,200}

local ServerStorage = game:GetService("ServerStorage")
local GasterTestMulti = ServerStorage:WaitForChild("GasterTestMulti")
local GasterTest1,GasterTest2,GasterTest3 = GasterTestMulti:WaitForChild("GasterTest1"),GasterTestMulti:WaitForChild("GasterTest2"),GasterTestMulti:WaitForChild("GasterTest3")
local SansBonezone = ServerStorage:WaitForChild("SansBonezone")
local Bones = ServerStorage:WaitForChild("Bones")

for _,player in pairs(Players) do
   local Char = player.Character or player.CharacterAdded:Wait()
   local Distance = (Char.HumanoidRootPart.Position -  Boss.HumanoidRootPart.Position).Magnitude
   if Distance < TargetInfo[2] then
      --Rest of code goes here
   end
end

Notes:
If you want to consistently check the distance between the player and Boss, you’d have to check when the player moves, by checking this thread.

Line 8, Still no dice why it doesn’t work

Alright I’ll check this out thank you.
edit : This checks if the humanoid is moving for my thing I just wanna see the closest player, then send the attack to them.