Attempt to index nil with 'HumanoidRootPart'

local pllayer2 = workspace:FindFirstChild("player2").Value
            if pllayer2 ~= nil then
            print(pllayer1)
            workspace:FindFirstChild(pllayer2).HumanoidRootPart.Position = workspace.Plate2.PlayerSpawn2.Position + Vector3.new(0, 3, 0)
            game.Players:FindFirstChild(pllayer2).Team = game.Teams.living
        end

image
image

says indexing with nil even though it checks if it is nil

assuming this “player2” object is in workspace and it somehow gets the name of player2…

local pllayer2 = workspace:FindFirstChild(workspace.player2.Value) --This finds the actual player in workspace with the name provided in the player2.Value
if pllayer2 then --Checks if the name is found in workspace.
	pllayer2.HumanoidRootPart.Position = workspace.Plate2.PlayerSpawn2.Position + Vector3.new(0, 3, 0)
	game.Players:FindFirstChild(pllayer2.Name).Team = game.Teams.living
end

Edit( you cant find the player using the player2 object. you have to use its name (i just fixed this))

it worked after i added if pllayer2 then cause it is always in workspace and also tell me what was wrong with my script

workspace:FindFirstChild(pllayer2)
assumes that the Value of the player2 StringValue object in your original script has a name that can be found in workspace. If it cant be found in workspace it would cause problems.

You should check whatever method you use to put player2’s name in the player2 StringValue Object.

this is how i do it.

local players = game:GetService("Players")
local function onPlayerAdded(player)
	local playerCount = #game.Players:GetPlayers()
	local playerinstance = workspace:FindFirstChild("player"..playerCount)
	print(playerinstance)
	local position = playerinstance
	print(position)
	position.Value = player.Name
	position.Parent = workspace
end

I think I understand what you are doing.
It looks like some kind of system to move players to a spawn.

Here is an alternative that should allow you to move all players to a spawn part or just one to a spawn.

It makes some assumptions that might not be true so take a look.
I left some notes in the script to point out and explain the assumption I made and function of some parts.

local OccupiedSpawns = {}
local Spawns = workspace.Plate2:GetChildren() --Assumes Plate2 is some kind of folder that has all of the spawn parts?

local function Spawn_To_Living(x)
	if x == "All" then
		table.clear(OccupiedSpawns)
		local Players = game.Players:GetPlayers()
		for i,Player in pairs(Players) do
			if Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
				local Moved = false
				for ii,Spawnn in pairs(Spawns) do
					if Spawnn.Name == "PlayerSpawn"..tostring(i) then
						Player.Character.HumanoidRootPart.Position = Spawnn.Position + Vector3.new(0, 3, 0)
						Player.Team = game.Teams.living
						table.insert(OccupiedSpawns,Spawnn)
						Moved = true
					end
				end
				if Moved == false then --Moving to random spawn because it could not find a spawn matching the number of the player.
					Player.Character.HumanoidRootPart.Position = Spawns[math.random(1,#Spawns)].Position + Vector3.new(0, 3, 0)
					Player.Team = game.Teams.living
				end
			end
		end
	elseif game.Players:FindFirstChild(x) then
		local Player = game.Players[x]
		if Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
			for i,Spawnn in pairs(Spawns) do
				if not table.find(OccupiedSpawns,Spawnn) then
					Player.Character.HumanoidRootPart.Position = Spawnn.Position + Vector3.new(0, 3, 0)
					Player.Team = game.Teams.living
					table.insert(OccupiedSpawns,Spawnn)
					break
				end
			end
		end
	end
end

Spawn_To_Living("All")
--This is a function that moves all players to the living team and moved them to seperate spawns if there is one for each player.
--Otherwise it moves the player to a random spawn if there are no other spawns with numbers for the player to go to.
Spawn_To_Living("PlayerName")
--This will do the same thing, but it will only apply to one player. For whatever you want I guess. Like PlayerAdded which you were doing.

I also assumed you have different spawns in Plate2 all with different numbers…
Example: PlayerSpawn1, PlayerSpawn2, PlayerSpawn3, PlayerSpawn4 etc.

1 Like