Attempt to index nil with 'Team'

  1. What do you want to achieve? That my script only runs if a player is on a certain team

  2. What is the issue? It gives this error: attempt to index nil with 'Team’

  3. What solutions have you tried so far? I’ve tried looking but nothing worked for some reason

This is not the whole script

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		if plr.Team == game.Teams.Police then
			print("Police!")
		end

I hope someone can help!

NOTE: It’s on a server script and plr is defined as this:

local Players = game:GetService("Players")
local plr = Players.LocalPlayer
function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		if plr.Team == game.Teams.Police then
			print("Police!")
		end
	end
end

Please tell me if this works :smiley:

Prob forgot multiple ends to fix the error

No like i said its not the whole script (sorry if this sound rude)

Mhm, can I see some of it? maybe I can see if anything is wrong we might need full script due to understanding some stuff of how the script works!

This is the whole script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local checkpoint = script.Parent
local plr = Players.LocalPlayer

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		if plr.Team == game.Teams.Police then
			print("Police!")
		end
				local player = Players:GetPlayerFromCharacter(hit.Parent)
				local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
				if not checkpointData then
					checkpointData = Instance.new("Folder")
					checkpointData.Name = "CheckpointData"
					checkpointData.Parent = ServerStorage
				end

				local userIdString = tostring(player.UserId)
				local checkpointValue = checkpointData:FindFirstChild(userIdString)
				if not checkpointValue then
					checkpointValue = Instance.new("ObjectValue")
					checkpointValue.Name = userIdString
					checkpointValue.Parent = checkpointData

				player.CharacterAdded:connect(function(character)
					wait()
					local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
					character:MoveTo(storedCheckpoint.Position + Vector3.new(math.random(-4, 4), 4, math.random(-4, 4)))
				end)
			end

			checkpointValue.Value = checkpoint
			end
end

checkpoint.Touched:Connect(onTouched)

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local checkpoint = script.Parent
local plr = Players.LocalPlayer

function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
		if plr.Team == game.Teams.Police then
			print("Police!")
		end
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Folder")
			checkpointData.Name = "CheckpointData"
			checkpointData.Parent = ServerStorage
		end

		local userIdString = tostring(player.UserId)
		local checkpointValue = checkpointData:FindFirstChild(userIdString)
		if not checkpointValue then
			checkpointValue = Instance.new("ObjectValue")
			checkpointValue.Name = userIdString
			checkpointValue.Parent = checkpointData

			player.CharacterAdded:connect(function(character)
				wait()
				local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
				character:MoveTo(storedCheckpoint.Position + Vector3.new(math.random(-4, 4), 4, math.random(-4, 4)))
			end)
		end

		checkpointValue.Value = checkpoint
	end
end

checkpoint.Touched:Connect(onTouched)

Player.LocalPlayer only works for the client-sided player.
If you want to get the player who touched the part use Players | Roblox Creator Documentation.
Basically:

local PlayerService = game:GetService("Players")

local function onTouched(hit)
   local plr = hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") and PlayerService:GetPlayerFromCharacter(hit.Parent)
   if plr and plr.Team == game:GetService("Teams").Police then
      print("Police!"
   end

Remove the plr variable by the way.

1 Like