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

How To Change Maps On CTF Roblox Template?

I used the Roblox CTF Template and i made multiple Maps on it, I dont know how to alternate the map, Basicly There is an Intermission that Runs, And after the intermission the game starts, I need to know how to alternate the Game Map After the Game time and before the Intermission!

local GameManager = require(script.GameManager)

GameManager:Initialize()

while true do
	repeat
		GameManager:RunIntermission()
	until GameManager:GameReady()
	
	GameManager:StopIntermission()
	GameManager:StartRound()	
	
	repeat
		GameManager:Update()
		wait()
	until GameManager:RoundOver()
	
	GameManager:RoundCleanup()
end

This Basically is the Main Game Code, Which runs and Repeats itself, I need help on the Map changing code, and where to put it!

1 answer

✓ Accepted answer

I believe that the top or bottom of your while true do loop would be suitable for the changing of maps. I think the top would suit best.

I don't know where your maps are located, so I'll be using (for these examples) Maps as a the path to a folder/model that would hold all the maps you want to switch.

Now, we could go about selection of a new map at random, or by order. I'll cover both, in case you wish to experiment.

local Maps -- Place the maps folder/model here. Example: game:GetService("ReplicatedStorage"):FindFirstChild("MapsFolder")


-- Random Map Selection:
local function GetRandomMap(maps)
	maps = maps or Maps -- So you can give the function a selection, instead of all of them (if you wanted)
	local children = maps:GetChildren() -- Get all maps that fall in the Maps parent
	return children[math.random(#children)] -- This will return the a random map, as it's selecting a random index of the children of `maps`
end


-- Ordered Map Selection:
local last_map = 0 -- We start at 0 and not 1, because it will be incremented before being used.
local function GetOrderedMap(maps)
	maps = maps or Maps
	last_map = (last_map or 0) + 1 -- This is telling last_map to set itself (if it wasn't set) and increment it by 1
	local children = maps:GetChildren()
	if last_map > #children then -- If we've cycled through all available maps....
		last_map = 1 -- Go back to first in list
	end
	return children[last_map] -- Give us our next map
end

Now that the selection process is complete, we would need a method of switching maps. I am going to try my best to explain the process.

local CurrentMap = nil -- We're going to store this variable so we can switch it out the next time the map changes

local function SwtichMap(map)
	if map and typeof(map) == "Instance" then -- Cant bring nil to the map, or anything we can't clone

		if CurrentMap then -- If we already have a map in wokspace...
			CurrentMap:Destroy() -- Make it go away.
		end

		local NewMap = map:Clone() -- We don't want to remove the original map out of the selections, so we Clone() it.

		if NewMap then -- Just to be sure
			NewMap.Parent = workspace -- The cloned map is placed in workspace, and ready for use.
			CurrentMap = NewMap -- Update current map to our new map
			return NewMap -- This is not necessary, but it's also not bad. Having the function return the new map would allow us to check if a map was chosen or not (fail-safe)
		end

	end
end

Now we can switch out a map like this. There are multiple different ways you could switch out a map, this is just one of many.

local map_selection = GetRandomMap() -- You could replace GetRandomMap with GetOrderedMap
while not map_selection do -- Oh no. The function did not return the map, that means one of the checks failed.
	map_selection = GetRandomMap() or GetOrderedMap() -- This allows for maximum flexibility. It's unlikely that this would happen unless 'Maps' is nil or you have no maps in 'Maps'
end
repeat
	CurrentMap = SwitchMap(map_selection) -- Keep trying
until CurrentMap -- Until its there

Now we got everything defined. We can begin placing code where it belongs. This is what I'd do

local GameManager = require(script.GameManager)

GameManager:Initialize()




---------- Please edit. ----------

local Maps -- Place the maps folder/model here! Example: game:GetService("ReplicatedStorage"):FindFirstChild("MapsFolder")

---------- Please edit. ----------

local last_map = 0
local CurrentMap = nil

local function GetRandomMap(maps)
	maps = maps or Maps
	local children = maps:GetChildren()
	return children[math.random(#children)]
end

local function GetOrderedMap(maps)
	maps = maps or Maps
	last_map = (last_map or 0) + 1
	local children = maps:GetChildren()
	if last_map > #children then
		last_map = 1
	end
	return children[last_map]
end

local function SwtichMap(map)
	if map and typeof(map) == "Instance" then
		if CurrentMap then
			CurrentMap:Destroy()
		end
		local NewMap = map:Clone()
		if NewMap then
			NewMap.Parent = workspace
			CurrentMap = NewMap
			return NewMap
		end
	end
end


while true do
	local map_selection = GetRandomMap()
	while not map_selection do
		map_selection = GetRandomMap() or GetOrderedMap()
	end
	repeat
		CurrentMap = SwitchMap(map_selection)
	until CurrentMap

	repeat
		GameManager:RunIntermission()
	until GameManager:GameReady()

	GameManager:StopIntermission()
	GameManager:StartRound()    

	repeat
		GameManager:Update()
	wait()
	until GameManager:RoundOver()

	GameManager:RoundCleanup()
end

2 comments

0

Thank you very much for your help

0

Happy to help.

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