"attempt to index nil with findfirstchild"

I keep getting this error on my code. I have been trying to fix it for a few days now but i havent been able to figure it out at all.

local function setUpHighlights(thisPlayer, character, role)
	local self = TeamController
	if role == Enums.IngameTeams.Hunters then
		if not character:FindFirstChild("HunterHighlight") then -- ERROR ATTEMPT TO INDEX NIL WITH FINDFIRSTCHILD
			
			if thisPlayer == player then
				self.MovementAnimationsController:OverrideAnimations(character, Enums.FlipEnums(Enums.IngameTeams)[role])
				self.AbilitiesController:RoleChanged()
				self.HighlightController:HighlightAllEnemies()
				
			else
				local data = self.DataController:GetData(thisPlayer)
				SharedFunctions.ResetAbilities(data)
				
			end
			
			createHighlight(character, Color3.new(0.72549, 0, 0.0117647))
			self.SoundController:Play("Switch")
		end
		
	else
		local highlight = character:FindFirstChild("HunterHighlight")
		if highlight then
			highlight:Destroy()
			
			if thisPlayer == player then
				self.MovementAnimationsController:OverrideAnimations(character)
				self.AbilitiesController:RoleChanged()
				
			else
				local data = self.DataController:GetData(thisPlayer)
				SharedFunctions.ResetAbilities(data)
				
			end
		end
		
	end
	
end

local function setUpPlayerUIs(team, teamList)
	local self = TeamController
	local template = IngameUI.PlayerFrame
	local myTeam, myTeamInfo = self:GetTeam(player)
	local gamemode = self.GamemodeController:GetGamemode()
	local gamemodeInfo = Gamemodes.NameOrder[gamemode]
	
	task.spawn(function()
		for _, thisPlayer: Player in teamList.Players do
			if not thisPlayer.Character or not thisPlayer.Parent then continue end
			local playerInfo = SharedFunctions.getPlayerAlive(teamList.PlayersAlive, thisPlayer)
			if not playerInfo then continue end
			self.WeaponController:CheckWeaponState(playerInfo) -- Errors here for character not existing
			setUpHighlights(thisPlayer, character, playerInfo and playerInfo.Role) -- Errors here for character not existing
			
			if thisPlayer == player then continue end
			local playerTeam, playerTeaminfo = self:GetTeam(thisPlayer)
			local clonedTemplate = PlayersHolder:FindFirstChild(tostring(thisPlayer.UserId))
			
			if not clonedTemplate then
				clonedTemplate = template:Clone()
				clonedTemplate.Name = thisPlayer.UserId
				clonedTemplate.PlayerName.Text = thisPlayer.Name
				if myTeamInfo == playerTeaminfo then
					clonedTemplate.LayoutOrder = 1
					
				else
					clonedTemplate.LayoutOrder = 2
					
				end
				
				clonedTemplate.Parent = PlayersHolder
				clonedTemplate.PlayerIcon.Image = Players:GetUserThumbnailAsync(thisPlayer.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
				
			end
			
			local runnerColour = Color3.new(1, 1, 1)
			local hunterColour = Color3.new(0.72549, 0, 0.0117647)
			if not playerInfo then
				clonedTemplate.LayoutOrder = myTeamInfo == playerTeaminfo and 17 or 18  -- if player died then this
				
				clonedTemplate.PlayerIcon.ImageTransparency = 0.3
				clonedTemplate.PlayerIcon.ImageColor3 = Color3.fromRGB(86, 86, 86)
				clonedTemplate.PlayerIcon.UIStroke.Transparency = 0.3
				clonedTemplate.PlayerIcon.UIStroke.UIGradient.Color = ColorSequence.new(Color3.fromRGB(85, 85, 85))
				
				clonedTemplate.PlayerName.TextTransparency = 0.3
				clonedTemplate.PlayerName.TextColor3 = Color3.fromRGB(86, 86, 86)
				
				if gamemodeInfo.MaxTeams then
					local teamColour = SharedVariables.TeamColours[team].Colour
					clonedTemplate.PlayerIcon.UIStroke.Color =  teamColour

				else
					clonedTemplate.PlayerIcon.UIStroke.Color = runnerColour

				end
				
				clonedTemplate.PlayerName.UIStroke.Transparency = 0.3
				
			else
				
				if playerInfo.Role == Enums.IngameTeams.Hunters then
					clonedTemplate.PlayerName.TextColor3 = hunterColour
					
					clonedTemplate.PlayerIcon.ImageTransparency = 0
					clonedTemplate.PlayerIcon.UIStroke.Transparency = 0
					clonedTemplate.PlayerName.TextTransparency = 0
					
					if gamemodeInfo.MaxTeams then
						local teamColour = SharedVariables.TeamColours[team].Colour
						clonedTemplate.PlayerIcon.UIStroke.Color = teamColour
						
					else
						clonedTemplate.PlayerIcon.UIStroke.Color = hunterColour
						
					end
					
				else
					
					clonedTemplate.PlayerName.TextColor3 = runnerColour
					
					clonedTemplate.PlayerIcon.ImageTransparency = 0.4
					clonedTemplate.PlayerIcon.UIStroke.Transparency = 0.4
					clonedTemplate.PlayerName.TextTransparency = 0.4
					
					if gamemodeInfo.MaxTeams then
						local teamColour = SharedVariables.TeamColours[team].Colour
						clonedTemplate.PlayerIcon.UIStroke.Color =  teamColour
						
					else
						clonedTemplate.PlayerIcon.UIStroke.Color = runnerColour
						
					end
					
				end
				
			end
			
			setUpLives(playerInfo, IngameUI.HealthPoint, clonedTemplate.PlayerHealthFrame)
			
			local overheadIcon = thisPlayer.Character:FindFirstChild("OverheadIcon")
			if overheadIcon then
				setUpLives(playerInfo, IngameUI.OverheadHeart, overheadIcon.Overhead.PlayerHealthFrame)
				
			end
			
		end
		
	end)
	
end

I put a comment in my code where the error happens. Since its an error that i cant replicate and only really happens in public servers its also really difficult to get the cause. Since im checking to see if the character exists before moving onto the next lines it doesn’t really make sense to me?

Thanks for any help i can get.

1 Like

apparently, character is nil. try printing character in the beginning of setUpHighlights and before you call it to see if there’s a difference

1 Like

Will do, its a hard bug to replicate so will be hard to tell if it worked or not. Do you have a theory of why it could be nil in one place but fine in the other (since it passed the if statement)?

1 Like

Where exactly is the “Character” variable defined?

are you doing it like this

local character = Player.Character

or like this?

local character = Player.Character or Player.CharacterAdded:Wait()

if your not use the second line of code, i suggest you do, so it waits for the character incase it did not load in on time.

1 Like
for _, thisPlayer: Player in teamList.Players do
			if not thisPlayer.Character or not thisPlayer.Parent then continue end
....

Im doing this. So it checks for thisPlayer.Character and continues if it doesnt exist. Doing characcterAdded:Wait() wouldnt really work here sadly. Thank you for the suggestion tho

2 Likes

I believe this is a race condition since you claim it’s hard to replicate. You check for the player.Character before an asynchronous call to retrieve the playerinfo, the chances of the player dying or respawning during the waiting period of the call may result in the error.

2 Likes
if not thisPlayer.Character or not thisPlayer.Parent then continue end

you’re not checking if the variable “character” is nil, just player.Character

1 Like

Ye, thats what i was also thinking. Testing, it doesnt look like there is any time delay between these 2 lines:

print(First check)|
if not thisPlayer.Character or not thisPlayer.Parent then continue end|
local character = thisPlayer.Character|
if not character then continue end|

local playerInfo = SharedFunctions.getPlayerAlive(teamList.PlayersAlive, thisPlayer)|
print(Second check)|

the getAlivePlayer function:

function sharedFunctions.getPlayerAlive(playersAlive, player)
	for index,playerInfo in playersAlive do
		if playerInfo.Player == player then
			return playerInfo, index
			
		end
		
	end
	
end
1 Like

thisPlayer.Character should check if character is nil

1 Like

Sure, i guess i could do that but it wont really solve the problem since it calls the function perfectly fine using local self = TeamController

1 Like

but you didn’t even define character

1 Like

Well I’m lost on how to use self then … my bad.

1 Like

Ye just checked and it prints fine

1 Like