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

attempt to index field '?' (a nil value)

I came across this error in the Output tab and I don't quite know what's the problem, it says that the problem is in the line 64 so I checked if I misspelled something but everything was written correctly. I don't know what causes this, I'm a new scripter so my knowledge about this is very limited.

-- Variables defined.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 50

local reward = 50

-- Waiting...

while true do 
	
	Status.Value = "We need more players to begin."	
	
	repeat wait(1) until game.Players.NumPlayers >= 1
	
	Status.Value = "Intermission" 
	
	wait(5)
	
	local plrs = {}
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then 
			table.insert(plrs,player) -- Add each player into plrs table.
			
		end
	end
	
	wait(2)
	
	local AvailableMaps = MapsFolder:GetChildren()
	
	local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]	
	
	Status.Value = ChosenMap.Name.." was Chosen!"	
	
	local ClonedMap = ChosenMap:Clone() 
 	ClonedMap.Parent = workspace	
	
	--Teleport players to that map.
	
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")	
	
	if not SpawnPoints then
		print("No Spawnpoints.")
	end
	
	local AvailableSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(plrs) do
		if player then
			character = player.Character
			
			if character then
				--Teleport
				
				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
				table.remove(AvailableSpawnPoints,1)

2 answers

Try using :WaitForChild instead of :FindFirstChild

1 comment

0

Nope, still shows that message.

character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame

There are two situations that would show that error.

The first is thatcharacter:FindFirstChild("HumanoidRootPart") returned nil because a child named HumanoidRootPart was not found in character. However, you the previous answer suggested you use WaitForChild instead of FindFirstChild, and you commented that it still shows the error, so this is not the issue.

The other situation is that AvailableSpawnPoints[1] is nil, so by doing AvailableSpawnPoints[1].CFrame, you're essentially doing nil.CFrame which is why the error says you're attempting to index a nil value.

AvailableSpawnPoints seems to be a table of the children of ClonedMap.SpawnPoints, where you remove each spawn point from the table whenever a player is teleported to that spawn point. So this is probably the issue. You've removed all the spawn points in AvailableSpawnPoints, causing AvailableSpawnPoints[1] to be nil as there are no more items inside AvailableSpawnPoints. You have to ensure that you have enough spawn points inside ClonedMap.SpawnPoints.

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