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)
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)
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)
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.
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.