Why does this return 1?

My goal is to make a script that when a 25 second intermission is over, will spawn a map that is randomly chosen.

However, local mapName will return 1 (there is only 1 map), saying "1 is not a valid member of Folder “ReplicatedStorage.Maps” - Server - GameHandler:6

I searched on Google, DevForum, and YouTube, but nothing fixed my issue, only making it worse

local gameStatus = game.ReplicatedStorage:WaitForChild("GameStatus")
local gameTimer = game.ReplicatedStorage:WaitForChild("TimerStatus")
local lobbyCFrame = workspace:WaitForChild("Lobby"):WaitForChild("SpawnLocation").CFrame + Vector3.yAxis*5
local maps = game.ReplicatedStorage.Maps:GetChildren()
local mapsLength = #maps
local random = math.random(1, mapsLength)
local mapName = game.ReplicatedStorage.Maps[random]
local INTERMISSION = 25

while true do
	for countDown = INTERMISSION, 0, -1 do
		gameStatus.Value = "Intermission"
		gameTimer.Value = countDown
		task.wait(1)
	end
	gameStatus.Value = "Starting Game"
	task.wait(5)
	gameStatus.Value = "Map Name: "..mapName
	task.wait(5)
	local map = mapName:Clone()
	map.Parent = workspace
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Because you’re indexing the instance itself, not selecting a random child of it.
Change line 7 to:

local chosenMap = maps[random] -- Instance
local mapName = chosenMap.Name -- The Instance's name.

and use these variables to load your desired map.

2 Likes

Instead of saying that last line, do:

local mapName = maps[random]

This is because game.ReplicatedStorage.Maps is an instance, not a table.

Didn’t see your post, sorry lol

Edit #2: Im being stupid ignore my last edit

This works, although how would I get the 3rd to last line, local map = mapName:Clone() to work? Sorry for the extra question

Use chosenMap instead of mapName for that

1 Like

This worked, thank you. limit

Make sure to mark TheRealANDRO’s post as the solution

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.