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

How do you fix a attempt to get length of upvalue 'maps' (a userdata value) error?

This is my script;

local replicatedStorage = game:GetService("ReplicatedStorage")
local maps = replicatedStorage:WaitForChild("Maps")
 
function pickMap()
	local mapList = maps:GetChildren() -- Declares a table of the maps inside of the Maps folder.
	local selectedIndex = math.random(1,#maps) -- Choose a random number between 1, and the number of maps available.
	local map = mapList[selectedIndex]:Clone() -- Create a clone of the map we selected.
	map.Parent = workspace -- Parent it to the workspace.
	map.Name = "Map" -- Rename the map so that we can use the unloadMap() function later to remove it.
	return mapList[selectedIndex].Name -- Return the name of the map we selected, in case we want to display it.
end
 
function unloadMap()
	if workspace:FindFirstChild("Map") then -- If there is a model in the Workspace named "Map"
		workspace.Map:Destroy() -- Destroy it!
	end
end
 
local h = Instance.new("Hint")
h.Parent = workspace
 
while true do
	h.Text = "Picking new map..."
	wait(3)
	local mapName = pickMap()
	h.Text = "Selected map: "..mapName
	wait(3)
	h.Text = "Unloading map..."
	unloadMap()
	wait(3)
end

This is the entire output;

22:32:56.305 - The Place I Learn To Script/Build was auto-saved to C:/Users/BlackOrange3343/Documents/ROBLOX/AutoSaves/Place_AutoSave_38.rbxl
Ragdoll Physics Loaded
Hello world!
22:32:59.642 - Humanoid is not a valid member of Script
22:32:59.644 - Stack Begin
22:32:59.645 - Script 'Plugin_144773946.RealRagdoll.Ragdoll', Line 4
22:32:59.646 - Stack End
Loading Cutscene Editor...
Bouyer's Ragdoll Physics Loaded
Finished Loading.
Loading Tree Gen...
Finished Loading Tree Gen
22:33:00.580 - Unable to load plugin icon. (x2)
Realism Mod is running on v1.795!
Cutscene Editor Loaded
Hello world!
22:33:03.719 - Workspace.MapChooser:6: attempt to get length of upvalue 'maps' (a userdata value)
22:33:03.721 - Stack Begin
22:33:03.723 - Script 'Workspace.MapChooser', Line 6 - global pickMap
22:33:03.724 - Script 'Workspace.MapChooser', Line 25
22:33:03.725 - Stack End
22:33:05.714 - Infinite yield possible on 'Workspace:WaitForChild("Explosion Course")'
22:33:05.718 - Stack Begin
22:33:05.720 - Script 'ServerScriptService.ExplodePartScript', Line 17
22:33:05.722 - Stack End

This is the error in the output;

22:33:03.719 - Workspace.MapChooser:6: attempt to get length of upvalue 'maps' (a userdata value)
22:33:03.721 - Stack Begin
22:33:03.723 - Script 'Workspace.MapChooser', Line 6 - global pickMap
22:33:03.724 - Script 'Workspace.MapChooser', Line 25
22:33:03.725 - Stack End

There are 2 lines with the error;

local selectedIndex = math.random(1,#maps) -- Choose a random number between 1, and the number of maps available.

and

local mapName = pickMap()

How can you fix this? Explain, thanks for any support, comments and answers!

1 comment

0

thank you for asking this question it helped me. :o)

1 answer

✓ Accepted answer

The problem is your use of #maps. You are referencing the folder when you say maps. Instead, you want to say the number of objects inside the folder

try this instead:

local selectedIndex = math.random(1,#mapList)

1 comment

0

Thanks! This worked and helped me improve!

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