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 3 years ago
Edited 3 years ago

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

while true do
    local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
    local maps = game.ReplicatedStorage:WaitForChild("Maps")
    wait(3)
    local chosen1 = maps[math.random(1,#maps:GetChildren())]
    chosen1.Parent = workspace.Maps
    wait()
    local chosen2 = maps[math.random(1,#maps:GetChildren())]
    chosen2.Parent = workspace.Maps
    wait()
    local chosen3 = maps[math.random(1,#maps:GetChildren())]
    chosen3.Parent = workspace.Maps
    chosen1:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("1").Value)
    chosen2:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("2").Value)
    chosen3:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("3").Value)
    for i = timer.Value,0,-1 do
        timer.Value = timer.Value - 1
    end
    for i,v in pairs(game.Workspace.Maps:GetChildren()) do
        v.Parent = maps
    end
    timer.Value = 90
end

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 — 3y
0
^ idiot DiamondComplex 285 — 3y

3 answers

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

Replace it to this for line 5

local maps = game.ReplicatedStorage.Maps:GetChildren()
local 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:

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

Hope this helps

Ad
Log in to vote
2
Answered by
Rinpix 639 Moderation Voter
3 years ago
Edited 3 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.

while true do
    local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
    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:

local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
local maps = game.ReplicatedStorage:WaitForChild("Maps")

while true do
    wait(3)
    local chosen1 = maps[math.random(1,#maps:GetChildren())]
    chosen1.Parent = workspace.Maps
    wait()
    local chosen2 = maps[math.random(1,#maps:GetChildren())]
    chosen2.Parent = workspace.Maps
    wait()
    local chosen3 = maps[math.random(1,#maps:GetChildren())]
    chosen3.Parent = workspace.Maps
    chosen1:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("1").Value)
    chosen2:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("2").Value)
    chosen3:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("3").Value)
    for i = timer.Value,0,-1 do
        timer.Value = timer.Value - 1
    end
    for i,v in pairs(game.Workspace.Maps:GetChildren()) do
        v.Parent = maps
    end
    timer.Value = 90
end

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.

local replicatedStorage = game:GetService("ReplicatedStorage")

local timer = replicatedStorage:WaitForChild("Information"):WaitForChild("Time")
local mapsWS = workspace:WaitForChild("Maps")
local mapsRS = replicatedStorage:WaitForChild("Maps")
local mapsTable = mapsInstance:GetChildren()
local positions = replicatedStorage:WaitForChild("Positions")

while true do
    wait(3)
    local chosen1 = mapsTable[math.random(1,#mapsTable)]
    chosen1.Parent = mapsWS
    wait()
    local chosen2 = mapsTable[math.random(1,#mapsTable)]
    chosen2.Parent = mapsWS
    wait()
    local chosen3 = mapsTable[math.random(1,#mapsTable)]
    chosen3.Parent = mapsWS
    chosen1:SetPrimaryPartCFrame(positions:WaitForChild("1").Value)
    chosen2:SetPrimaryPartCFrame(positions:WaitForChild("2").Value)
    chosen3:SetPrimaryPartCFrame(positions:WaitForChild("3").Value)
    for i = timer.Value,0,-1 do
        timer.Value = timer.Value - 1
    end
    for i,v in pairs(mapsWS:GetChildren()) do
        v.Parent = mapsRS
    end
    timer.Value = 90
end

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:

math.random(1,#mapsTable)

is redundant.

Put this instead:

math.random(#mapsTable)

The result is exactly the same.

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

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

You can just do this:

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

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

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

So, the final code should look like this:

local replicatedStorage = game:GetService("ReplicatedStorage")

local timer = replicatedStorage:WaitForChild("Information"):WaitForChild("Time")
local mapsWS = workspace:WaitForChild("Maps")
local mapsRS = replicatedStorage:WaitForChild("Maps")
local mapsTable = mapsInstance:GetChildren()
local positions = replicatedStorage:WaitForChild("Positions")

while true do
    wait(3)
    local chosen1 = mapsTable[math.random(#mapsTable)]
    chosen1.Parent = mapsWS
    local chosen2 = mapsTable[math.random(#mapsTable)]
    chosen2.Parent = mapsWS
    local chosen3 = mapsTable[math.random(#mapsTable)]
    chosen3.Parent = mapsWS
    chosen1:SetPrimaryPartCFrame(positions:WaitForChild("1").Value)
    chosen2:SetPrimaryPartCFrame(positions:WaitForChild("2").Value)
    chosen3:SetPrimaryPartCFrame(positions:WaitForChild("3").Value)
    for i = timer.Value, 0, -1 do
        timer.Value = i
        wait(1)
    end
    for i,v in pairs(mapsWS:GetChildren()) do
        v.Parent = mapsRS
    end
    timer.Value = 90
end

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 3 years ago

My take:

while true do
    local timer = game.ReplicatedStorage:WaitForChild("Information"):WaitForChild("Time")
    local maps = game.ReplicatedStorage:WaitForChild("Maps")
    wait(3)
    local chosen1 = maps:GetChildren()[math.random(1,#maps:GetChildren())]
    chosen1.Parent = workspace.Maps
    wait()
    local chosen2 = maps:GetChildren()[math.random(1,#maps:GetChildren())]
    chosen2.Parent = workspace.Maps
    wait()
    local chosen3 = maps:GetChildren()[math.random(1,#maps:GetChildren())]
    chosen3.Parent = workspace.Maps
    chosen1:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("1").Value)
    chosen2:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("2").Value)
    chosen3:SetPrimaryPartCFrame(game.ReplicatedStorage.Positions:WaitForChild("3").Value)
    for i = timer.Value,0,-1 do
        timer.Value = timer.Value - 1
    end
    for i,v in pairs(game.Workspace.Maps:GetChildren()) do
        v.Parent = maps
    end
    timer.Value = 90
end

Answer this question