Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index local error, fix? [ SOLVED ]

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.