Attempt to index nil with "FindFirstChild"

  1. What do you want to achieve? I tried to make a system where all enemies haves their stats by individual module that its named the enemy’s name, idk if you get it but here’s what i meant
    Captura de pantalla 2024-08-29 104335

  2. What is the issue? I get the error ServerScriptService.Main.Enemy:156: attempt to index nil with 'FindFirstChild' and comes from this line: local enemyStats = require(enemyModuleAccess:FindFirstChild(name)), and this is how i use it: enemy.Spawn("Moonling", 6 * enemyCountMultiplier, 1.5, map)

  3. What solutions have you tried so far? I dont know how to fix this and theres no similar issue no mine i guess except this error

local currentGameMode = workspace.Info.CurrentGamemode
local enemyModuleFolder = ServerScriptService.EnemyModule
local enemyModuleAccess

if currentGameMode and currentGameMode.Value == "Easy" then
	enemyModuleAccess = enemyModuleFolder.EasyMode
elseif currentGameMode and currentGameMode.Value == "Normal" then
	enemyModuleAccess = enemyModuleFolder.NormalMode
elseif currentGameMode and currentGameMode.Value == "Hard" then
	enemyModuleAccess = enemyModuleFolder.HardMode
end

function enemy.Spawn(name, quantity, interval, map)
	for i = 1, quantity do
		local enemyStats = require(enemyModuleAccess:FindFirstChild(name))
		task.wait(interval)
		local enemyExists
		if currentGameMode and currentGameMode.Value == "Easy" then
			enemyExists = ServerStorage.Enemies.EasyMode:FindFirstChild(name)
		elseif currentGameMode and currentGameMode.Value == "Normal" then
			enemyExists = ServerStorage.Enemies.NormalMode:FindFirstChild(name)
		elseif currentGameMode and currentGameMode.Value == "Hard" then
			enemyExists = ServerStorage.Enemies.HardMode:FindFirstChild(name)
		end

		if enemyExists then
			local newEnemy = enemyExists:Clone()
			newEnemy.PrimaryPart.CFrame = map.Start.CFrame
			newEnemy.Parent = workspace.Enemies

			enemy.Optimize(newEnemy)

			local movingTo = Instance.new("IntValue")
			movingTo.Name = "MovingTo"
			movingTo.Value = 1
			movingTo.Parent = newEnemy.Config

			for i, object in ipairs(newEnemy:GetDescendants()) do
				if object:IsA("BasePart") or object:IsA("MeshPart") then
					object.CollisionGroup = "Enemy"
				end
			end

			local health = enemyStats["Health"]
			newEnemy:SetAttribute("Health", health)

			newEnemy:GetAttributeChangedSignal("Health"):Connect(function()
				if newEnemy:GetAttribute("Health") <= 0 then
					enemy.Die(newEnemy)
				end
			end)

			coroutine.wrap(enemy.Move)(newEnemy, map)
		end
	end
end

None of these branches are equating to true which means enemyModuleAccess is never actually assigned to. You need to make sure your instance value has a value before assigning to enemyModuleAccess.

You need to wait for enemyModuleAccess to be registered in the hierarchy using :WaitForChild

local enemyModuleAccess = 
game:GetService("serviceName"):WaitForChild("enemyModuleAccess");

Or wherever its located. Also as @12345koip said, it may be a conditional issue where none of the branches are equating to true

1 Like

This one is not an issue with the script not loading. It’s an uninitialized constant and then is assigned to within a selection statement, but none of the branches equate to true, hence it stays nil. WaitForChild is not needed in this script because it’s a server script and the instances have all loaded by the time the script runs. WaitForChild tends to only be needed on the client, unless an instance is created during runtime a script must wait for.

1 Like

For Debugging you should add a else statment, incase currentGameMode isn’t asigned.

else 
    warn("The enemy module hasn't been set.")
    enemyModuleAccess = enemyModuleFolder.EasyMode
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.