Attempt to index nil with 'Health'

So, my tower script, has a reload function. if an enemy is in range after reloading, it errors out Attempt to index nil with 'Health'. any help?

		for i, plrs in ipairs(game.Players:GetPlayers()) do 
			if target then
				if target:FindFirstChild("Humanoid").Health < config.Damage.Value then --error here
					plrs.leaderstats.Money.Value += target:FindFirstChild("Humanoid").Health
				else
					plrs.leaderstats.Money.Value += config.Damage.Value
				end
			end
		end

Humanoid is found from player.Character.

        for i, plrs in ipairs(game.Players:GetPlayers()) do 
			if target then
				if target.Character:FindFirstChild("Humanoid").Health < config.Damage.Value then --error here
					plrs.leaderstats.Money.Value += target.Character:FindFirstChild("Humanoid").Health
				else
					plrs.leaderstats.Money.Value += config.Damage.Value
				end
			end
		end

its printing out Character is not a valid member of Model "Workspace.Mobs.NormalZombie". target is not a player.

Which line is it coming from? The comparing if line or the plrs.leaderstats.Money.Value += line?

the if line.

limit

Why not try :FindFirstChildOfClass instead?

                for i, plrs in ipairs(game.Players:GetPlayers()) do 
			if target then
				if target:FindFirstChildOfClass("Humanoid").Health < config.Damage.Value then --error here
					plrs.leaderstats.Money.Value += target:FindFirstChildOfClass("Humanoid").Health
				else
					plrs.leaderstats.Money.Value += config.Damage.Value
				end
			end
		end

If it doesn’t work, can you show me a screenshot of the children listed under your npc?

ok it works! thx.

edit: it doesnt

nevermind, apparently not. It worked for one of the tries but not the second one.
Screenshot 2023-09-19 135723

I found out the problem! the target doesnt change when it reloads, so when the target dies, its still looking for it.

1 Like

Yeah, scripting errors are very easy to make.

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