Help with script error

Can someone help me with this error im getting on line 6. It happens when the correct team member passes through the door without getting killed.

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	local Teams = game:GetService("Teams")
	local team = Teams["team1"]
	 if player.Team ~= team then
		character.Humanoid.Health = 0
	end
end)

here is the error

" Workspace.worldobjects.teamdoor.Script:6: attempt to index nil with ‘Team’ - Server - Script:6 "

I am assuming i need something in the script that recognizes the player as someone who shouldnt be killed and skips the rest of the script.

I tried something like this

if player.Team == team then return end

But it didnt work.

The problem is, the team is nil

I dont know how to fix that. I am self taught and pretty new.

The problem I think is what its touching is not a player, fix this by doing:

if game.Players:GetPlayerFromCharacter(hit.Parent) then --If there is a player, do this
         --Code
end

Yeah, this is the solution, you just need to check if “player” is actually a player instance and not nil before you attempt to index its “Team” property.

The script works if the player is not team1. It killed them without error. It only gives the error when team1 walks through it. The script functions as it should, but drops that error if team1 player goes through and doesnt get killed as he shouldnt.

local players = game:GetService("Players")
local teams = game:GetService("Teams")

local part = script.Parent

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			if hitPlayer.Team ~= teams.team1 then
				local humanoid = hitModel:FindFirstChildOfClass("Humanoid")
				if humanoid then
					humanoid.Health = 0
				end
			end
		end
	end
end)

This will kill any player who isn’t on the “team1” team if they touch the killing part.

1 Like