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

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
Status = ReplicatedStorage.Status

local chosenmap = script.MapChosen.Value
local maps = ServerStorage.Maps:GetChildren()

function chosemap()
    local map = math.random(1,#maps)
    local cmap = map:Clone()
    cmap.Parent = workspace
    chosenmap = cmap.Name
end

while true do

    for i = 5,0,-1 do
    Status.Value = i
    wait(1)
end
 chosemap()
  Status.Value = "Selecting Map...."
  wait(2)
  Status.Value = "Selected Map: " .. chosenmap 
    wait(60)
    end



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 7 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)]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
Status = ReplicatedStorage.Status

local chosenmap = script.MapChosen.Value
local maps = ServerStorage.Maps:GetChildren()

function chosemap()
    local map = maps[math.random(1,#maps)]
    local cmap = map:Clone()
    cmap.Parent = workspace
    chosenmap = cmap.Name
end

while true do

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

    chosemap()
    Status.Value = "Selecting Map...."
    wait(2)
    Status.Value = "Selected Map: " .. chosenmap 
    wait(60)
end

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

I figured it out ;3

0
Awh User#11440 120 — 7y

Answer this question