Why does it return nil?

  1. What do you want to achieve?

I want to get information from this line about how much HP the opponent has in the script module.

local attributeName = game.ServerStorage.Enemies[enemyName]
  1. What is the issue?
    I wanted to get the opponent’s name from the module, but it doesn’t work and return nil…

Snippet from script:

for _,spawnPoint in pairs(spawnerlist) do
	if game.Workspace.Level1.Respawns then
		local respawnTime = spawnPoint:GetAttribute("RespawnTime")
		local enemyName = spawnPoint:GetAttribute("EnemyName") -- returns the name of the opponent

		local attributeName = game.ServerStorage.Enemies[enemyName]
		local healthEnemy = nil
		CombatSettings.Settings.Mobs[attributeName] = healthEnemy

		spawnEnemy(spawnPoint, nil, respawnTime, attributeName)

Snippet from ModuleScript:

local CombatSettingsPlayer = {
		["Mobs"] = {
			["Knight"] = {
				["Statistic"] = {
					["MaxHealth"] = 80,
					["Health"] = 80,
					["Speed"] = 20,

return CombatSettingsPlayer
  1. What solutions have you tried so far?
    I read the Roblox forum, looked at the wiki and looked for information on YouTube, but I didn’t find anything…
1 Like

Does respawnTime or enemyName return nil? Or is it only when you attempt to get that attribute’s value?


Wait so if the attributeName is returning something then what’s not working?

The name attribute returns me the opponent’s name, but when I try in this line:

		local healthEnemy = nil
		CombatSettings.Settings.Mobs[attributeName] = healthEnemy

This is when this attributeName changes to nil, because of this I can’t select my opponent’s name from module script

healthEnemy is essentially nil so the script seems to just be setting the table to nil…

uh maybe try setting it to a. Table or something else

Here I have 2 examples of what is happening

example 1:
When he tries to get to HP

example 2:
When he’s just trying to get to my enemy

I used the tables as you wrote and nothing changed, unfortunately

1 Like

Could you potentially provide a ss of the Mobs folder?

And maybe try using FindFirstChild instead of uh [] square brace notation

attributeName is more than likely an instance based on how the path is defined. You should use enemyName instead of attributeName when you Index the dictionary.

1 Like

There is progress, it has detected how much HP it should have, but it still throws an error with nil…

What does the spawnEnemy function look like?

function spawnEnemy(spawnPoint, spawnedEnemy, respawnTime, attributeName, healthEnemy)
	spawnedEnemy = attributeName:Clone()
    spawnedEnemy.HumanoidRootPart.CFrame = spawnPoint.CFrame + Vector3.new(0,3,0)
	spawnedEnemy.Parent = game.Workspace.Level1.Enemy
	spawnedEnemy.EnemyHumanoid.Health = healthEnemy

    local humanoidConnection = nil

    humanoidConnection = spawnedEnemy:WaitForChild("EnemyHumanoid").Died:Connect(function()
			spawnEnemy(spawnPoint, spawnedEnemy, respawnTime, attributeName, healthEnemy)

This seems good. So I’ll ask about the previous block of code. Is the snippet of that code all of what is inside of the for loop? Do you reference CombatSettings.Settings.Mobs elsewhere as well?

1 Like

Okey, I fixed the problem, it was related to the script not being able to detect the “Archer” class because there was a typo in ModulScript, which caused it to return nil because it couldn’t find such a name


Thank you all for your help!

1 Like

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