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

Attempt to index nil with name and parent?

Asked by
CodeWon 181
3 years ago
Edited 3 years ago

The script I supposed to chose a random map and spawn it in. But I got a few errors.

"Attempt to index nil with 'Name' "

Then I changed it a little and got

Attempt to index nil with 'Parent' "

Script:

local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")

while true do

    status.Value = "Intermission"
    wait(10)

    local Maps = MapsFolder:GetChildren()

    local randomMap = math.random(1,#Maps)

    Status.Value = "Map chosen: "..randomMap.Name

    randomMap.Parent = game.Workspace
end

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

It is because randomMap value is a random number between 1 to how much children inside maps, BUT number does not have a name, or a parent. It's a string, not an instance. So what are you gonna do is replace line 11 to:

local randomMap = Maps[math.Random(1,#Maps)]
0
Now it's an instance! Xapelize 2658 — 3y
0
Thanks a lot! CodeWon 181 — 3y
0
No problem!!! Xapelize 2658 — 3y
Ad

Answer this question