Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[URGENT] Attempt to Index nil with "1"?

Asked by 4 years ago
Edited 4 years ago

I am trying to make a game like the corridor of hell. This is the script I came up with:

01while true do
02    local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
03    local maps = game.ReplicatedStorage:WaitForChild("Maps")
04    wait(3)
05    local chosen1 = maps[math.random(1,#maps:GetChildren())]
06    chosen1.Parent = workspace.Maps
07    wait()
08    local chosen2 = maps[math.random(1,#maps:GetChildren())]
09    chosen2.Parent = workspace.Maps
10    wait()
11    local chosen3 = maps[math.random(1,#maps:GetChildren())]
12    chosen3.Parent = workspace.Maps
13    chosen1:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("1").Value)
14    chosen2:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("2").Value)
15    chosen3:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("3").Value)
View all 23 lines...

It is erroring on line 5. Here is a screenshot of my explorer: https://gyazo.com/8640fdb0d6591b0cb069ee0c2347c2e3

Please help as I am doing something very urgent. Thanks

0
^ no WideSteal321 773 — 4y
0
^ idiot DiamondComplex 285 — 4y

3 answers

Log in to vote
3
Answered by
naturedat 124
4 years ago
Edited 4 years ago

Replace it to this for line 5

1local maps = game.ReplicatedStorage.Maps:GetChildren()
2local chosen1 = maps[math.random(1,#maps)]

Did you put the maps in a folder named Maps? Well, you need to use GetChildren instead of WaitForChild. Or if you just put the map in the Replicated Storage then:

1local maps = game.ReplicatedStorage:GetChildren()
2local chosen1 = maps[math.random(1,#maps)]

Hope this helps

Ad
Log in to vote
2
Answered by
Rinpix 639 Moderation Voter
4 years ago
Edited 4 years ago

While naturedat's response is perfectly sufficient in terms of correcting your errors, it doesn't detail exactly what and why what you did was wrong, so I thought I'd weigh in and provide a more thorough explanation. And provide a lot of conventional practices as well.

1while true do
2    local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
3    local maps = game.ReplicatedStorage:WaitForChild("Maps")

First off, you're defining the variables at the beginning of each loop, which is fine, but since they'll never change, it's probably better to just put them before the while loop.

So, it should look like this:

01local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
02local maps = game.ReplicatedStorage:WaitForChild("Maps")
03 
04while true do
05    wait(3)
06    local chosen1 = maps[math.random(1,#maps:GetChildren())]
07    chosen1.Parent = workspace.Maps
08    wait()
09    local chosen2 = maps[math.random(1,#maps:GetChildren())]
10    chosen2.Parent = workspace.Maps
11    wait()
12    local chosen3 = maps[math.random(1,#maps:GetChildren())]
13    chosen3.Parent = workspace.Maps
14    chosen1:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("1").Value)
15    chosen2:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("2").Value)
View all 24 lines...

Second, you're attempting to index an instance as though it is a table. You need to call the GetChildren function on the instance to be able to index it, because that function returns a table, containing all of the objects inside of the instance you called GetChildren on. Since you didn't do that when indexing the maps variable, you receive an error, because you were trying to index an instance, and not a table.

I notice that later on in the script you use the maps variable in the context of putting the maps inside of two different instances that go by the name "Maps", one in Workspace and one in ReplicatedStorage. So I think you'll need to set up three variables, one referring to the Maps instance inside of ReplicatedStorage, another referring to the Maps instance inside of Workspace, and one more referring to the table returned by calling GetChildren on the Maps instance inside of ReplicatedStorage. As a matter of fact, you might as well add a variable for the "Positions" instance inside of ReplicatedStorage as well, since you use it multiple times. And for ReplicatedStorage too.

I'm creating variables for each of these things because it can get a little unwieldy to type "workspace.Maps" or "game.ReplicatedStorage.Positions" repeatedly. It just makes things a lot easier and helps to clean up your code a bit.

01local replicatedStorage = game:GetService("ReplicatedStorage")
02 
03local timer = replicatedStorage:WaitForChild("Information"):WaitForChild("Time")
04local mapsWS = workspace:WaitForChild("Maps")
05local mapsRS = replicatedStorage:WaitForChild("Maps")
06local mapsTable = mapsInstance:GetChildren()
07local positions = replicatedStorage:WaitForChild("Positions")
08 
09while true do
10    wait(3)
11    local chosen1 = mapsTable[math.random(1,#mapsTable)]
12    chosen1.Parent = mapsWS
13    wait()
14    local chosen2 = mapsTable[math.random(1,#mapsTable)]
15    chosen2.Parent = mapsWS
View all 29 lines...

I replaced a lot of the repeated uses of code with the variables defined just before the while loop and it's already looking a lot cleaner. I know this is way over the top and much more than you asked, but I tend to value intricate corrections over simpler ones. Or maybe I just have OCD. Anyways, there are still some things to note.

When calling the math.random function, you can just pass in one value and it'll automatically return a random value between 1 and the value you passed in, so putting this:

1math.random(1,#mapsTable)

is redundant.

Put this instead:

1math.random(#mapsTable)

The result is exactly the same.

This bit of code right here isn't really problematic, but as opposed to this:

1for i = timer.Value,0,-1 do
2    timer.Value = timer.Value - 1
3end

You can just do this:

1for i = timer.Value,0,-1 do
2    timer.Value = i
3end

You also need to add a wait function inside of that loop so that the loop doesn't fully execute in an instant:

1for i = timer.Value,0,-1 do
2    timer.Value = i
3    wait(1)
4end

So, the final code should look like this:

01local replicatedStorage = game:GetService("ReplicatedStorage")
02 
03local timer = replicatedStorage:WaitForChild("Information"):WaitForChild("Time")
04local mapsWS = workspace:WaitForChild("Maps")
05local mapsRS = replicatedStorage:WaitForChild("Maps")
06local mapsTable = mapsInstance:GetChildren()
07local positions = replicatedStorage:WaitForChild("Positions")
08 
09while true do
10    wait(3)
11    local chosen1 = mapsTable[math.random(#mapsTable)]
12    chosen1.Parent = mapsWS
13    local chosen2 = mapsTable[math.random(#mapsTable)]
14    chosen2.Parent = mapsWS
15    local chosen3 = mapsTable[math.random(#mapsTable)]
View all 28 lines...

I know I went way overboard with this answer, but I just like to give very informative responses whenever I answer questions on this website, lol. If I don't, it just makes me feel like I'm farming reputation points.

Log in to vote
0
Answered by 4 years ago

My take:

01while true do
02    local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
03    local maps = game.ReplicatedStorage:WaitForChild("Maps")
04    wait(3)
05    local chosen1 = maps:GetChildren()[math.random(1,#maps:GetChildren())]
06    chosen1.Parent = workspace.Maps
07    wait()
08    local chosen2 = maps:GetChildren()[math.random(1,#maps:GetChildren())]
09    chosen2.Parent = workspace.Maps
10    wait()
11    local chosen3 = maps:GetChildren()[math.random(1,#maps:GetChildren())]
12    chosen3.Parent = workspace.Maps
13    chosen1:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("1").Value)
14    chosen2:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("2").Value)
15    chosen3:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("3").Value)
View all 23 lines...

Answer this question