Attempt to index nil with "Team"

I have an explosive barrel script and I’m trying to prevent it from teamkilling.
This error “Attempt to index nil with Team” pops up every time the barrel explodes near a player, and I don’t know how to get rid of it.

local model = script.Parent
local part = model.Head
local humanoid = model:WaitForChild("Humanoid")
local d = game:GetService("Debris")

local lastPlayer = nil

local connection

connection = humanoid.HealthChanged:Connect(function(health)
	local tag = humanoid:FindFirstChild("WeaponTag")
	if tag then
		lastPlayer = tag.Value
	end
	if health <= 0 then
		local e = Instance.new("Explosion")
		e.Name = "BarrelExplosion"
		e.BlastRadius = 30
		e.BlastPressure = 300000
		e.DestroyJointRadiusPercent = 0
		e.Position = part.Position
		e.Hit:Connect(function(hit)
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum then
				local player = game.Players:GetPlayerFromCharacter(hum.Parent)
				if (player.Team ~= lastPlayer.Team) or (player == lastPlayer) then --line where error occurs
					local weaponTag = Instance.new("ObjectValue")
					weaponTag.Name = "WeaponTag"
					weaponTag.Value = lastPlayer
					weaponTag.Parent = hum
					hit.Parent:BreakJoints()
				end
			end
		end)
		e.Parent = workspace
		
		model.Parent.Respawn:Fire()
		d:AddItem(model, 0)
		connection:Disconnect()
	end
end)

Before the if statement that errors, check if lastPlayer exists.

Still doesn’t work. I think nil is referring to player.Team.

I believe that tag is returning as nil, therefore lastPlayer is never being set to tag.Value.
Try printing tag and let me know your result.

Tag is returning as a player.
(below damage remote line is what tag prints out)

Is player being returned as the player as well?
What happens when you try printing,

print(player.Team)
print(lastPlayer.Team)

I highly doubt it’ll return the same output, otherwise script would be working.
Once we figure out which one is wrong - we can solve this.

Would something like this work?

local model = script.Parent
local part = model.Head
local humanoid = model:WaitForChild("Humanoid")
local d = game:GetService("Debris")

local lastPlayer = nil

local connection

connection = humanoid.HealthChanged:Connect(function(health)
	local tag = humanoid:FindFirstChild("WeaponTag")
	if tag then
		lastPlayer = tag.Value
	end
	if health <= 0 then
		local e = Instance.new("Explosion")
		e.Name = "BarrelExplosion"
		e.BlastRadius = 30
		e.BlastPressure = 300000
		e.DestroyJointRadiusPercent = 0
		e.Position = part.Position
		e.Hit:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") ~= nil then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if (player.Team ~= lastPlayer.Team) or (player == lastPlayer) then --line where error occurs
					local weaponTag = Instance.new("ObjectValue")
					weaponTag.Name = "WeaponTag"
					weaponTag.Value = lastPlayer
					weaponTag.Parent = hit.Parent.Humanoid
					hit.Parent:BreakJoints()
				end
			end
		end)
		e.Parent = workspace
		model.Parent.Respawn:Fire()
		d:AddItem(model, 0)
		connection:Disconnect()
	end
end)

Prints out my name and the team, yet the error still appears.

Overlooked something, this is the first set that prints out. It prints lastPlayer and lastPlayer.Team but not player/player.Team.

Odd. The team is a “Team” Instance of Teams’ Service correct?
Have you tried comparing the TeamColor?

if player.TeamColor == lastPlayer.TeamColor then

I’ve had an issue using team names in the past, try comparing TeamColor’s.

The fix to your issue might even be something like

` if player.Team.Name == lastPlayer.Team.Name’

Not 100% confident though.

The thing is this line:

local player = p:GetPlayerFromCharacter(hum.Parent)

is returning nil, so I can’t even get the TeamColor from the player if it’s nil.

Can you paste this code in and let me know what output returns?
Might even help you solve the problem.

local model = script.Parent
local part = model:WaitForChild("Head")
local humanoid = model:WaitForChild("Humanoid")
local d = game:GetService("Debris")

local lastPlayer = nil

local connection

connection = humanoid.HealthChanged:Connect(function(health)
	local tag = humanoid:FindFirstChild("WeaponTag")
	if tag then
		print(tag.Value.. "'s tag has been found.")
		lastPlayer = tag.Value
	end
	if health <= 0 then
		local e = Instance.new("Explosion")
		e.Name = "BarrelExplosion"
		e.BlastRadius = 30
		e.BlastPressure = 300000
		e.DestroyJointRadiusPercent = 0
		e.Position = part.Position
		e.Hit:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") ~= nil then
				print("Humanoid Found. ")
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				print(player.. " has been found.")
				print(hit.Parent.. " was character model found.")
				if (player.Team ~= lastPlayer.Team) or (player == lastPlayer) then --line where error occurs
					print(player.Team.. " is the new player's team.")
					print(lastPlayer.Team.. " is the last player's team.")
					if player==lastPlayer then
						print("player = last player ")
					end
					local weaponTag = Instance.new("ObjectValue")
					weaponTag.Name = "WeaponTag"
					weaponTag.Value = lastPlayer
					print(weaponTag.Value)
					if hit.Parent:FindFirstChild("Humanoid") then
						weaponTag.Parent = hit.Parent.Humanoid
						hit.Parent:BreakJoints()
					end
				end
			end
		end)
		e.Parent = workspace
		model.Parent.Respawn:Fire()
		d:AddItem(model, .01)
		connection:Disconnect()
	end
end)

WELL, I just found out why it prints that error.

I printed the humanoid’s parent and,

the explosion detected the barrel’s humanoid instance and attempted to get a player out of that. So the script actually works, I just need to filter the barrel humanoid out.

Thanks for your help, though.

No problem. In the future, just try using print() to get the script to talk to you and tell you what’s wrong with it. Very useful tool for debugging, as I’m sure you know.