Attempt to index nil with "Team"

Hello! I’ve tried to script a part which automatically kills anyone who is on the team “NEMO” (it also rotates thanks to HingeConstraints). However, I get this error.
obrazek
I’m not really sure what’s wrong, because the player variable should be correct therefore there should be a Team property which can be checked.

The script:

local SoundService = game:GetService("SoundService")
local FailSound = SoundService:WaitForChild("CourseFail")
for i,v in pairs(workspace:GetChildren()) do
	if v.Name == "KillBlock" then
		KillBlock = v
	end	
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.FailGUI

KillBlock.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
	if Humanoid then
		if player.Team == game.Teams.NEMO then
		    Humanoid.Health = 0
			FailSound:Play()
			local Highlight = Instance.new("Highlight")
			Highlight.Parent = hit.Parent
			Event:FireAllClients(hit)
		end
	end
end)
2 Likes

in the if statement add and player:

if Humanoid and player then
2 Likes

Thanks for your response! However, it still doesn’t work, unfortunately.

1 Like

Ensure that Team NEMO actually exists before attempting to index it, by using :FindFirstChild()

2 Likes

Unfortunately, it still doesn’t work. However thanks for your response.

The way I did it: (Might be just mine mistake)

local team = game.Teams:FindFirstChild("NEMO")

FailHitbox.Touched:Connect(function(hit)
------
1 Like

Does any sort of error occur once it happens, or does nothing at all happen?

You could try this piece of code I wrote, but I am not sure if it’s going to work…

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Teams = game:GetService("Teams")

local event = ReplicatedStorage.FailGUI
local failSound = SoundService:WaitForChild("CourseFail")

local killBlock
for _, child in workspace:GetChildren() do
	if child.Name ~= "KillBlock" then
		continue
	end
	
	killBlock = child
	continue
end

local function onTouched(hitPart: BasePart)
	local hitCharacter = hitPart:FindFirstAncestorOfClass("Model")
	if not hitCharacter then
		return
	end
	
	local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
	if not hitHumanoid or hitHumanoid.Health <= 0 then
		return
	end
	
	local playerHit = Players:GetPlayerFromCharacter(hitCharacter)
	if not playerHit or playerHit.Team ~= Teams.NEMO then
		return
	end
	
	hitHumanoid.Health = 0
	failSound:Play()
	
	local highlight = Instance.new("Highlight")
	highlight.Parent = hitCharacter
	
	event:FireAllClients(hitPart)
end

killBlock.Touched:Connect(onTouched)
2 Likes

I tested your script and it seems to work, I’m guessing the player somehow returned as nil? Try using the function :GetPlayerFromCharacter() instead of finding the player in the players properties?

3 Likes

I don’t get the error now, but the player doesn’t die. Do you think you know how that can be fixed?

1 Like

Is there only one instance in workspace named KillBlock? If so, you could replace the loop with workspace:FindFirstChild("KillBlock"). Otherwise? It may be getting the wrong instance.

1 Like

Nope, there are numerous instances called “KillBlock”.

1 Like

Well that may or may not explain it…

The fix should be this:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Teams = game:GetService("Teams")

local event = ReplicatedStorage.FailGUI
local failSound = SoundService:WaitForChild("CourseFail")

local function onTouched(hitPart: BasePart)
	local hitCharacter = hitPart:FindFirstAncestorOfClass("Model")
	if not hitCharacter then
		return
	end

	local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
	if not hitHumanoid or hitHumanoid.Health <= 0 then
		return
	end

	local playerHit = Players:GetPlayerFromCharacter(hitCharacter)
	if not playerHit or playerHit.Team ~= Teams.NEMO then
		return
	end

	hitHumanoid.Health = 0
	failSound:Play()

	local highlight = Instance.new("Highlight")
	highlight.Parent = hitCharacter

	event:FireAllClients(hitPart)
end

for _, child in workspace:GetChildren() do
	if child.Name ~= "KillBlock" then
		continue
	end

	child.Touched:Connect(onTouched)
end
2 Likes

Yes, works perfectly. Thank you so much!

2 Likes

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