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

Tried to make random map system.but it throws an error attempt to index nil with 'Name'. What to do?

Server script (in serverscriptservice):


local lobby_Duration = 10
local round_Duration = 10
local maps = game:GetService('ReplicatedStorage'):WaitForChild('Map'):GetChildren()
local timerval = game.ReplicatedStorage:WaitForChild("TimerValue")

local function RTimer ()
	while wait() do
		for l = lobby_Duration, 0, -1 do
			timerval.Value = 'Intermission: '..l
			wait(1)

			if l == 0 then
				local map = maps[math.random(#maps, 1)] -- change to your map name
				timerval.Value = 'Teleporting players to '..map.Name
				wait(1)
				map.Parent = workspace

				local players = game.Players:GetChildren()

				for i = 1, #players do
					players[i].Character:MoveTo(Vector3.new(-122.5, 55.5, -41.5))
				end

				wait(1)

				for g = round_Duration,0, -1 do
					timerval.Value = 'Round '..g..' seconds left'
					wait(1)

					if g == 0 then
						timerval.Value = 'Round over'
						wait(2)
						timerval.Value = 'Teleporting players to lobby'
						wait(1)
						for i = 1, #players do
							players[i].Character:MoveTo(Vector3.new(-122.5, 55.5, 2.5))
						end

						map.Parent = maps
						wait(1)
						-- repeat again
					end
				end
			end
		end
	end
end

spawn(RTimer)

1 answer

The reason for this is on the line where you choose a random map, math.random should have a min argument that is lesser than the max argument, you are doing the opposite, that's why it's not choosing a map and returning nil, the fix would probably be to swap the two values

if l == 0 then
	local map = maps[math.random(1, #maps)] -- this is the problem
	nametimerval.Value = 'Teleporting players to '..map.Name
	-- Your code

3 comments

0

Then it returns an error invalid argument #2 to 'random' (interval is empty)

0

Could it be line 3 where you havent defined what map is? im talking about ' local maps = '

0

@Br0kenM1ndss that means that your maps folder has 0 children (maps) inside it

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