Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index nil with 'HumanoidRootPart' ?

I Want players in a table to teleport in a specific position. But, the output says that HumanoidRootPart Is Nil.

And I have made a table and saw,

[1] = Brioche_Noodle [2] = "*** cycle table reference detected ***"

Function That I am focusing on,

function spawnMap()
	local Maps = Storage.Maps:GetChildren()
	chosenMap = Maps[math.random(1, #Maps)]
	
	chosenMap:Clone()
	chosenMap.Parent = workspace.Map
	
	
	for _, player in ipairs(Playing) do
		if player then
			print(player)
			player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
		end
	end

end

Here is my whole script.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Storage = game:GetService("ServerStorage")

local Playing = {}
local NotPlaying = {}
local Infected = {}

local Team = {
	Lobby = Teams.Lobby,
	Infected = Teams.Infected,
	Citizens = Teams.Citizens
}
local gameStarted = false
local chosenMap

local function onPlayerAdded(player)
	print(player.Name .. " || Joined || ")
	
	if gameStarted == false then 
		print(player.Name .. " is added to The Playing Table")

		table.insert(Playing, player)
		print(Playing)
	else
		print(player.Name .. " is added to The Not Playing Table")

		table.insert(NotPlaying, player)
		print(NotPlaying)
	end
	player.Team = Team.Lobby
end



local function addPlayerToPlaying()
	local AllPlayers = Players:GetPlayers()
	
	table.insert(Playing, Playing)
end

local function addPlayerToNotPlaying()
	local AllPlayers = Players:GetPlayers()

	table.insert(NotPlaying, Playing)
end

function spawnMap()
	local Maps = Storage.Maps:GetChildren()
	chosenMap = Maps[math.random(1, #Maps)]
	
	chosenMap:Clone()
	chosenMap.Parent = workspace.Map
	
	
	for _, player in ipairs(Playing) do
		if player then
			print(player)
			player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
		end
	end

end

function despawnMap()
	local Maps = Storage.Maps:GetChildren()
	chosenMap.Parent = nil
	
	for _, player in ipairs(Playing) do
		if player then
			player.CharacterAdded:Connect(function(Character)
				Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Lobby.Teleporter.Position)
			end)
		end
	end
	
end

-- Listeners 

Players.PlayerAdded:Connect(onPlayerAdded)

-- Script

while true do
	gameStarted = false	
	
	wait(4)
	
	gameStarted = true
	addPlayerToPlaying()
	spawnMap()
	wait(10)
	despawnMap()
	
	Playing = nil
	NotPlaying = nil
	Infected = nil
end

3 comments

0

addPlayerToPlaying/NotPlaying does not do as their title says. Why are you inserting the Playing array? 

0

FYI, line 57/70 is redundant.

0

despawnMap fails to live up to its title, too

2 answers

✓ Accepted answer

Change your spawnMap() function to the following:

function spawnMap()
	local Maps = Storage.Maps:GetChildren()
	chosenMap = Maps[math.random(1, #Maps)]

	chosenMap:Clone()
	chosenMap.Parent = workspace.Map
 

	for _, player in ipairs(Playing) do
		if player then
			print(player.Name.." is in Playing table")
			local char = player.Character or player.CharacterAdded:Wait()
			if char then
				print(player.Name.."'s character exists")
				player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
			end
		end
	end
end

I tested the script in my own place, so this should work. If you still get the same (or a different) error, get back to me and I'll see if I can help more.

6 comments

0

It's best to ignore the player if they're dead. Sending them dead to the game or waiting 'til they respawn can/will yield undesirable results.

0

You're also building a Vector3 from a Vector3? Doing so will result in 0, 0, 0.

0

I've changed the, HumanoidRootPart.Position = Vector3.new() into, HumanoidRootPart.CFrame = CFrame.New() And It Works. But, the Output Says that :Wait() Is Nil.

0

I've changed the, HumanoidRootPart.Position = Vector3.new() into, HumanoidRootPart.CFrame = CFrame.New() And It Works. But, the Output Says that :Wait() Is Nil.

0

I removed the Charcter added And It worked!

0

Ah good to hear.

You need to check the player's character still exists as well as the player

This discussion is preserved as it was. New answers, comments, and votes are closed.