Attempt to get length of a Instance value

Hey, i’m new to scripting. Can anyone tell me what is the problem with script? I cant figure it out.

Paths:

Script path

path2

Map Location screenshot

local RS = game:GetService("ReplicatedStorage")
local GearFolder = RS:WaitForChild("Gears"):GetChildren()

local MapHolder = RS:WaitForChild("MapHolder")


function chooseSpawn()
	for i,v in pairs(MapHolder:GetChildren())  do
		local RandomMap = v[math.random(1,#v)]
		local MapWorkspace = game.Workspace[RandomMap]:FindFirstChild(v.Name)
		
		local spawns = MapWorkspace:WaitForChild("Spawns"):GetChildren()
		if spawns  then		
		local _Spawn = spawns[math.random(1,#spawns)]

		return _Spawn	
		end
	end
end

local function chooseItem ()
	local item = GearFolder[math.random(1,#GearFolder)]

	return item
end

while task.wait(5) do
	local _spawn = chooseSpawn()
	local item = chooseItem()
	local clone = item:Clone()

	clone.Parent = game.Workspace
	clone.Handle.CFrame = _spawn.CFrame * CFrame.new(0,5,0)
end
local RandomMap = v[math.random(1,#v)]

Did you try this?

local RandomMap = math.random(1,#v)

I just tried it , but it still throws an error. Thanks for replying.

Try this maybe?

GameMaps = {}
for _,Maps in pairs(MapHolder:GetChildren()) do
	table.insert(GameMaps, Maps)
end

for i,v in pairs(GameMaps) do
-- Map code
end

Still gives an error, or i did a mistake?

local RS = game:GetService("ReplicatedStorage")
local GearFolder = RS:WaitForChild("Gears"):GetChildren()

local MapHolder = RS:WaitForChild("MapHolder")

GameMaps = {}


function chooseSpawn()
	for _,Maps in pairs(MapHolder:GetChildren())  do
		table.insert(GameMaps, Maps)
		
		for i,v in pairs(GameMaps) do
			-- Map code
		local RandomMap = math.random(1,#Maps)
		local MapWorkspace = game.Workspace[RandomMap]:FindFirstChild(Maps.Name)
		
		local spawns = MapWorkspace:WaitForChild("Spawns"):GetChildren()
		if spawns  then		
		local _Spawn = spawns[math.random(1,#spawns)]

				return _Spawn	
			end	
		end
	end
end

local function chooseItem ()
	local item = GearFolder[math.random(1,#GearFolder)]

	return item
end

while task.wait(5) do
	local _spawn = chooseSpawn()
	local item = chooseItem()
	local clone = item:Clone()

	clone.Parent = game.Workspace
	clone.Handle.CFrame = _spawn.CFrame * CFrame.new(0,5,0)
end



Try this:

local RS = game:GetService("ReplicatedStorage")
local GearFolder = RS:WaitForChild("Gears"):GetChildren()

local MapHolder = RS:WaitForChild("MapHolder")


function chooseSpawn()
	for i,v in pairs(MapHolder:GetChildren())  do
		local RandomMap = MapHolder:GetChildren()[math.random(1,#MapHolder:GetChildren())]
		local MapWorkspace = game.Workspace[RandomMap]:FindFirstChild(v.Name)
		
		local spawns = MapWorkspace:WaitForChild("Spawns"):GetChildren()
		if spawns  then		
		local _Spawn = spawns[math.random(1,#spawns)]

		return _Spawn	
		end
	end
end

local function chooseItem ()
	local item = GearFolder[math.random(1,#GearFolder)]

	return item
end

while task.wait(5) do
	local _spawn = chooseSpawn()
	local item = chooseItem()
	local clone = item:Clone()

	clone.Parent = game.Workspace
	clone.Handle.CFrame = _spawn.CFrame * CFrame.new(0,5,0)
end

Your initial problem:

v is an instance. With the first v variable defined, you’re indexing a part with a number. As with @ewdsfdfdfdwedwerww 's solution, you should be using :GetChildren() instead to get the random map:

local children = MapHolder:GetChildren()
local RandomMap = children[math.random(1, #children)]

Hello, thanks for replying. It throws another error.
error3

What are you using MapWorkspace for? I don’t know

Its to find that map in workspace.

If the map in Workspace is already defined, use this instead:

local RS = game:GetService("ReplicatedStorage")
local GearFolder = RS:WaitForChild("Gears"):GetChildren()

local MapHolder = RS:WaitForChild("MapHolder")

function chooseSpawn()
	local RandomMap = MapHolder:GetChildren()[math.random(1, #MapHolder:GetChildren())]
	local MapWorkspace = workspace:FindFirstChild(RandomMap.Name)
	
	local spawns = RandomMap:WaitForChild("Spawns"):GetChildren()
	if spawns then		
		local _Spawn = spawns[math.random(1, #spawns)]

		return _Spawn
	end
end

local function chooseItem ()
	local item = GearFolder[math.random(1,#GearFolder)]

	return item
end

while task.wait(5) do
	local _spawn = chooseSpawn()
	local item = chooseItem()
	local clone = item:Clone()

	clone.Parent = game.Workspace
	clone.Handle.CFrame = _spawn.CFrame * CFrame.new(0,5,0)
end

Your initial problems:

You’re indexing an entire instance (RandomMap), when it is expecting a string. You would get the map’s name instead:

local MapWorkspace = game.Workspace:FindFirstChild(RandomMap.Name)

You don’t need the above function to get the random map since you’re already getting the random map inside.

2 Likes

Ohh thanks i get it now, but it throws this error?

error5

Make sure a folder called Spawns exists in RocketArena.

There is.

image

The script is looking for a folder called Spawns, not SpawnFolder. So rename the folder to Spawns or change line 10 in the script to this:

local spawns = RandomMap:WaitForChild("SpawnsFolder"):GetChildren()
1 Like

Thanks, my mistake the script works now on all maps!

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