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

Alternating maps using CTF Template?

I already made a couple maps, and I put them into a Folder called Maps, which is in ReplicatedStorage. Apparently the script that handles the map is in game.ServerScriptService.MainGameScript.GameManager.MapManager. Here's the code of that script:

local MapManager = {}

-- Local Variables
local MapSave = Instance.new('Folder', game.ServerStorage)
MapSave.Name = 'MapSave'

-- Initialization

local MapPurgeProof = game.Workspace:FindFirstChild('MapPurgeProof')
if not MapPurgeProof then
    MapPurgeProof = Instance.new('Folder', game.Workspace)
    MapPurgeProof.Name = 'MapPurgeProof'
end

-- Functions

function MapManager:SaveMap()
    for _, child in ipairs(game.Workspace:GetChildren()) do
        if not child:IsA('Camera') and not child:IsA('Terrain') and not child:IsA('Folder') then
            local copy = child:Clone()
            if copy then
                copy.Parent = MapSave
            end 
        end
    end
end

function MapManager:ClearMap()
    for _, child in ipairs(game.Workspace:GetChildren()) do
        if not child:IsA('Camera') and not child:IsA('Terrain') and not child:IsA('Folder') then
            child:Destroy()
        end
    end
end

function MapManager:LoadMap()
    spawn(function()
        for _, child in ipairs(MapSave:GetChildren()) do
            local copy = child:Clone()
            copy.Parent = game.Workspace
        end
    end)
end

return MapManager

I don't really see how the function SaveMap is that useful, since I'm going to be alternating maps. I just don't know what I should do, or if the answer is super obvious and I'm just not getting it.

1 answer

Here's what I've tried so far;

local MapManager = {}

-- Local Variables
local MapSave = Instance.new('Folder', game.ServerStorage)
MapSave.Name = 'MapSave'
local Maps = game.ReplicatedStorage.Maps:GetChildren()
local randomMap = math.random(1, #Maps)

-- Initialization

local MapPurgeProof = game.Workspace:FindFirstChild('MapPurgeProof')
if not MapPurgeProof then
	MapPurgeProof = Instance.new('Folder', game.Workspace)
	MapPurgeProof.Name = 'MapPurgeProof'
end

-- Functions

function MapManager:SaveMap()
	for _, child in ipairs(game.Workspace:GetChildren()) do
		if not child:IsA('Camera') and not child:IsA('Terrain') and not child:IsA('Folder') then
			local copy = child:Clone()
			if copy then
				copy.Parent = MapSave
			end	
		end
	end
end

function MapManager:ClearMap()
	for _, child in ipairs(game.Workspace:GetChildren()) do
		if not child:IsA('Camera') and not child:IsA('Terrain') and not child:IsA('Folder') then
			child:Destroy()
		end
	end
	game.Workspace.MapsInGame:ClearAllChildren() --tried to delete any map that's in the folder
end

function MapManager:LoadMap()
	--[[spawn(function()
		for _, child in ipairs(MapSave:GetChildren()) do
			local copy = child:Clone()
			copy.Parent = game.Workspace
		end
	end)--]]
	local currentMap = game.ReplicatedStorage.Maps:GetChildren(math.random(1, #Maps)) --tried to get a random map from the folder
	local insertMap = currentMap:Clone() --tried to clone the map
	insertMap.Parent = game.Workspace.MapsInGame --tried to put the map into the workspace (in a folder)
end
	
return MapManager

However, this time the map isn't even being inserted into the workspace. No errors.

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