Attempt to index nil with 'TeamColor'

this code

local players = game:GetService("Players")

while task.wait() do
	if game.Workspace.HealthPack then
	local touching = game.Workspace:GetPartsInPart(script.Parent)
	for i, v in pairs(touching) do
		if v.Parent:FindFirstChild("Humanoid") ~= nil then
			local playerFromCharacter = players:GetPlayerFromCharacter(v)
			if playerFromCharacter.TeamColor.Color == game.Workspace.HealthPack.circle.Color then
				v.Parent:FindFirstChild("Humanoid").Health = v.Parent:FindFirstChild("Humanoid").Health + 1.25
				wait(0.03)
			end
		end
	end
end
end

keeps giving me the Attempt to index nil with ‘TeamColor’ error.
image

I have tried to check some other articles using the if not player then return code but it didn’t give me a error or anything, so am unsure on how to fix it.

local players = game:GetService("Players")

while task.wait() do
	if game.Workspace.HealthPack then
		local touching = game.Workspace:GetPartsInPart(script.Parent)
		for i, v in pairs(touching) do
			if v.Parent:FindFirstChild("Humanoid") ~= nil then
				local playerFromCharacter = players:GetPlayerFromCharacter(v)
				if playerFromCharacter.TeamColor.Color == game.Workspace.HealthPack.circle.BrickColor then -- BrickColor instead of Color
					v.Parent:FindFirstChild("Humanoid").Health = v.Parent:FindFirstChild("Humanoid").Health + 1.25
					wait(0.03)
				end
			end
		end
	end
end

I replaced the code but it still shows the same exact error.

You need to check if the player exists.

Fixed code:

--//Services
local Players = game:GetService("Players")

--//Loops
while task.wait() do
	local touching = workspace:GetPartsInPart(script.Parent)
	
	for i, part in pairs(touching) do
		local player = Players:GetPlayerFromCharacter(part.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
		
		if humanoid and player.TeamColor.Color == workspace.HealthPack.circle.Color then
			humanoid.Health += 1.25
			
			task.wait(0.03)
		end
	end
end

That’s because this variable returns nil. Maybe an NPC was in a specific area or something.

You have to add an extra condition

if playerFromCharacter and playerFromCharacter.TeamColor == game.Workspace.HealthPack.circle.BrickColor then
1 Like

I have checked @Katrist and @R_obotz solutions but no errors show and both don’t work.

Its something to do with checking if the player exists, and it always thinks the player does not exist for some reason.

Could you show your explorer of where the script is located?

Because you put v instead of v.Parent as the parameter. Instead of putting the character as the parameter, you put a part inside the character, which will always return false

I already made mine with part.Parent so I don’t know why it wouldn’t work.

It’s kind of weird as theres a ability where it spawns in a heal field from server storage. The ability script is in StarterCharacterScripts and when the player uses the keyblind it will spawn the heal field using a remote event

image

It could be a problem with referencing the team color.

Try this:

--//Services
local Players = game:GetService("Players")

--//Loops
while task.wait() do
	local touching = workspace:GetPartsInPart(script.Parent)
	
	for i, part in pairs(touching) do
		local player = Players:GetPlayerFromCharacter(part.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
		print(script.Parent.Parent.circle.Color) --//Delete if it works after testing
		if humanoid and player.TeamColor.Color == script.Parent.Parent.circle.Color then
			humanoid.Health += 1.25
			
			task.wait(0.03)
		end
	end
end

Wait a minute, does the script have to be a local script?

No, make it a server script. Also, are you sure you’re setting the circle colour right? It’s most likely a problem with color matching.

In the ability script I set the color of the circle to the player teamcolor so most likely yes.

No errors, didnt add the health, printed out the color.

Oh I think I found out the problem. You did player.TeamColor.Color which gives the RGB value, not the BrickColor value.

Try this:

--//Services
local Players = game:GetService("Players")

--//Loops
while task.wait() do
	local touching = workspace:GetPartsInPart(script.Parent)
	
	for i, part in pairs(touching) do
		local player = Players:GetPlayerFromCharacter(part.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")

		if humanoid and player.TeamColor == script.Parent.Parent.circle.Color then
			humanoid.Health += 1.25
			
			task.wait(0.03)
		end
	end
end

Also, if you could send a .rbxm file with the healthpack, I could help out much quicker.

Could you also paste the script that sets the color of the healthpack?

HealField.rbxm (17.9 KB)

this is the event the ability script fires:

game.ReplicatedStorage.HealthDeployed.OnServerEvent:Connect(function(player)

	local tweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quad)

	local DIST_FROM_PLAYER = 0
	local char = player.Character or player.CharacterAdded:Wait()
	local Circle = game.ServerStorage.HealthPack:Clone()
	Circle.Parent = workspace
	Circle:SetPrimaryPartCFrame(CFrame.new(char.PrimaryPart.CFrame.Position+(DIST_FROM_PLAYER*char.PrimaryPart.CFrame.LookVector)))
	Circle.circle.Color = player.TeamColor.Color

	Circle.circle.ParticleEmitter.Color = ColorSequence.new{
		ColorSequenceKeypoint.new(0, player.TeamColor.Color),
		ColorSequenceKeypoint.new(1, player.TeamColor.Color)
	}

	Circle.circle.Sound:Play()

	wait(5.5)
	Circle.circle.Sound2:Play()
	tweenService:Create(Circle.circle, tweenInfo, {Transparency = 1}):Play()
	wait(.5)
	Circle:Destroy()
end)

This is the part where it changes color of the circle

TeamColor is found in player?