Hey, i’m new to scripting. Can anyone tell me what is the problem with script? I cant figure it out.
Paths:
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 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
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
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)]
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.