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

Attempt to index local error, fix? [ SOLVED ]

Asked by 8 years ago
Edited 8 years ago

I am making a game and I keep getting this error:

11:31:09.326 - Workspace.GameScript:10: attempt to index local 'map' (a number value) 11:31:09.330 - Stack Begin 11:31:09.332 - Script 'Workspace.GameScript', Line 10 - global chosemap 11:31:09.369 - Script 'Workspace.GameScript', Line 21 11:31:09.371 - Stack End

Here is the script:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local ServerStorage = game:GetService("ServerStorage")
03Status = ReplicatedStorage.Status
04 
05local chosenmap = script.MapChosen.Value
06local maps = ServerStorage.Maps:GetChildren()
07 
08function chosemap()
09    local map = math.random(1,#maps)
10    local cmap = map:Clone()
11    cmap.Parent = workspace
12    chosenmap = cmap.Name
13end
14 
15while true do
View all 26 lines...

I've been making scripts like this for a while and I have no IDEA why this is happening, I fixed it before but I forgot ;-;

2 answers

Log in to vote
0
Answered by 8 years ago

You're not picking the map correctly

math.random returns a number, not a Instance. To do what you want you want to index a table with this method. So line 6 should be local map = maps[math.random(1,#maps)]

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local ServerStorage = game:GetService("ServerStorage")
03Status = ReplicatedStorage.Status
04 
05local chosenmap = script.MapChosen.Value
06local maps = ServerStorage.Maps:GetChildren()
07 
08function chosemap()
09    local map = maps[math.random(1,#maps)]
10    local cmap = map:Clone()
11    cmap.Parent = workspace
12    chosenmap = cmap.Name
13end
14 
15while true do
View all 27 lines...

Good Luck!

If I helped, please don't forget to click the accept answer button next to my answer.
Ad
Log in to vote
0
Answered by 8 years ago

I figured it out ;3

0
Awh User#11440 120 — 8y

Answer this question