Why am I getting the error - Players.SadWowow21.Backpack.ClassicSword.Handle.Script:4: attempt to index nil with 'TeamColor'

I have no clue why it’s giving the error 08:45:59.270 - Players.SadWowow21.Backpack.ClassicSword.Handle.Script:4: attempt to index nil with ‘TeamColor’
because it was working earlier, but now it is.

This is the script I used before and I am using now.

debounce = false
function onTouch(part) 
	local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
	local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)--change this to get the player it is being used by.
	if hitplayer.TeamColor ~= hitter.TeamColor then
		local humanoid = part.Parent:FindFirstChild("Humanoid") 
		if debounce == false then
			if (humanoid ~= nil) then
				humanoid.Health = humanoid.Health - 35
				debounce = true
			end 
			if debounce == true then
				wait(2)
				debounce = false
			end
		end
	end
end

script.Parent.Touched:connect(onTouch)

You need to check if the part that touched the block is a part of a character model to begin with. :GetPlayerFromCharacter() returns nil if it can’t find any results. Use that result in an if statement to check if part that is touched is a character model part or not then continue with the rest of the code depending on that result.

2 Likes

Would I use the if statement like this?

debounce = false
function onTouch(part) 
	local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
	local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)--change this to get the player it is being used by.
	if hitplayer ~= nil then
	if hitplayer.TeamColor ~= hitter.TeamColor then
		local humanoid = part.Parent:FindFirstChild("Humanoid") 
		if debounce == false then
			if (humanoid ~= nil) then
				humanoid.Health = humanoid.Health - 35
				debounce = true
			end 
			if debounce == true then
				wait(2)
				debounce = false
				end
			end
		end
	end
end

script.Parent.Touched:connect(onTouch)

Yes, it should work if you use it like that.

1 Like

Would there be a way for it to damage players though?

It should be able to damage the player just like that.

it doesn’t - 30 - characters -

What do you try to achieve with the “hitter” variable?

if hitplayer.TeamColor ~= hitter.TeamColor then
He is trying to achieve this thing.

And one little question, is this script inside a tool?

1 Like

You should use Humanoid:TakeDamage(35) instead. This function allows ForceFields to protect against the damage source

Here’s a compressed version of your script with an easy fix for your error

debounce = false
function onTouch(part) 
	local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
	local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)--change this to get the player it is being used by.
	if hitplayer and hitter and hitplayer.TeamColor ~= hitter.TeamColor then
		local humanoid = part.Parent:FindFirstChild("Humanoid") 
		if humanoid and not debounce then
			humanoid:TakeDamage(35)
			debounce = true
			wait(2)
			debounce = false
		end
	end
end

script.Parent.Touched:connect(onTouch)

You just want to make sure both hitplayer and hitter are valid objects before you try to index TeamColor. The error is happening because you are trying to read the TeamColor property of a nil value when one of the player variables is missing.

4 Likes

Actually, there is a easier way to do it; just use a local script, get the local player who is currently holding it (“hitter”) and then fire a remoteEvent to add the damage to the player who touched it.

I tried that and it did not work

Please explain how it didn’t work if it really didn’t, such as supplying console logs or pictures/videos of what’s going on. One sentence isn’t sufficient enough to understand what went wrong in your script. Try doing some debugging as well.

Do you have a guaranteed hierarchy? Frankly a wild idea and why you shouldn’t be putting scripts for tools anywhere except directly under the tool instance - having that to reference from would make this much easier to track down, since the equipping character would only be a parent away.

local hitter = game:GetService("Players"):GetPlayerFromCharacter(script:FindFirstAncestorOfClass("Tool").Parent)
1 Like