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 a nil value?

I am trying to make a script that gets a random "Chunk" which is a group/model in a folder in server storage, The problem is that when it tries to pick a random "Chunk" it has this error code: Workspace.ObbyGenerator:9: attempt to index a nil value

Here is the script (It is a regular script in game.Workspace):

local ChunkSize = 144
local ChunkCounter = 0
local Chunks = {game.ServerStorage.Chunks:GetChildren()}

math.randomseed(123)

function CreateNewChunk()
	local ChunkCoordinate = ChunkSize * ChunkCounter
	local ChunkPicked = game.ServerStorage:FindFirstChild(Chunks[math.random(1, #Chunks)]):Clone()
	ChunkPicked.Parent = game.Workspace.Obby
	ChunkPicked:MoveTo(Vector3.new(ChunkCoordinate + 144, 0.5, 0))
	ChunkCounter = ChunkCounter + 1
end
	
local Obby = Instance.new("Model", game.Workspace)
	Obby.Name = "Obby"
local firsthundredchunks = 100
repeat
	firsthundredchunks = firsthundredchunks - 1
	ChunkCounter = ChunkCounter + 1
	wait()
until firsthundredchunks <= 0

while wait(3) do
	CreateNewChunk()
	ChunkCounter = ChunkCounter + 1
end

2 comments

0

game.ServerStorage.Chunks:GetChildren() returns a table, not a tuple

0

which means the Chunks table has one index and it is a table, not an object

2 answers

✓ Accepted answer

ooooh I get it now. What you are doing is requesting a table of children of an object by calling GetChildren() aka:

children = obj:GetChildren()
=
{
	1 = (all of obj's parent's).obj.Chunk,
	2 = (all of obj's parent's).obj.Chunk,
}

When you are using FindFirstChild you just provide a string a name and not an object (which you called by Chunks[math.random(1, #Chunks)])

So you can either FindFirstChild(chunk.Name) (not recommended) or you can even better just do the first one as it's aka

local chunkPicked = Chunks[math.random(1, #Chunks)]:Clone()

And don't forget to account for lucyy's answer or else both will not work.

2 comments

0

thanks i didnt catch that

0

When I try accepting both of your answers it doesn't let me do both at once :/

Remove the brackets on line 3

local Chunks = game.ServerStorage.Chunks:GetChildren()

What you're currently saying is get the children (which is a table), but you're storing the children in another table. Visually, this is what your problem looks like:


--Chunks table
Chunks = {
	(another table containing all the game.ServerStorage.Chunks children)
}

print(Chunks[1]) -- prints the game.ServerStorage.Chunks:GetChildren() table which is inside the Chunks table

5 comments

0

If you have any more problems with your script, let me know and I would be happy to help.

0

I tried removing the brackets and it still says the same error :/

0

When I try accepting both of your answers it doesn't let me do both at once :/

0

no worries.

0

just glad you got help the points are irrelevant

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