How to add new maps to my Sword Fighting Game

I would like my Sword Fighting Game to have maps. My Sword Fighting Game is functioning properly, i just think its a bit bland.

I tried adding a new map, but it didnt work.
image
Here is my game script (Props to
NoParameters
for helping me with this script)

local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local mapsFolder = ServerStorage:WaitForChild("Maps")
local statusTag = ReplicatedStorage:WaitForChild("Status")

-- Constants
local GAME_LENGTH = 50
local REWARD = 25
local PLAYERS_REQUIRED = 2
local INTERMISSION_TIME = 10

-- Functions
local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			if CollectionService:HasTag(player, "Alive") then
				CollectionService:RemoveTag(player, "Alive")
			end
		end)
	end
	if player.Character then
		onCharacterAdded(player.Character)
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

-- Main loop
while true do
	-- Waiting for players
	statusTag.Value = "Waiting for enough players"
	repeat
		task.wait(1)
	until
	#Players:GetPlayers() >= PLAYERS_REQUIRED

	-- Intermission
	for i = INTERMISSION_TIME, 0, -1 do
		statusTag.Value = "Intermission ["..i.."]"
		task.wait(1)
	end

	-- Choose a map
	local allMaps = mapsFolder:GetChildren()
	local map = allMaps[math.random(#allMaps)]:Clone()
	map.Parent = workspace
	statusTag.Value = map.Name .. " was chosen"
	task.wait(3)

	-- Teleport players
	local activePlayers = Players:GetPlayers()
	local spawns = map:FindFirstChild("SpawnPoints")
	if not spawns then
		warn("You do not have 'SpawnPoints', fix pl0x")
	end
	spawns = spawns:GetChildren()

	for _, player in pairs(activePlayers) do
		local hrp = player.Character:FindFirstChild("HumanoidRootPart")
		if hrp then
			local chosenSpawn = spawns[math.random(#spawns)]
			hrp.CFrame = chosenSpawn.CFrame
			table.remove(spawns, table.find(spawns, chosenSpawn))

			-- Give them a sword
			local sword = ServerStorage.ClassicSword:Clone()
			sword.Parent = player.Backpack

			CollectionService:AddTag(player, "Alive")
		end
	end

	statusTag.Value = "Get ready to play!"
	task.wait(2)

	for i = GAME_LENGTH, 0, -1 do
		if #CollectionService:GetTagged("Alive") == 1 then
			-- only one player is alive
			local winner = CollectionService:GetTagged("Alive")[1]
			statusTag.Value = "The winner is "..winner.Name
			winner.leaderstats.Bucks.Value += REWARD
			break
		elseif #CollectionService:GetTagged("Alive") == 0 then
			-- nobody is alive
			statusTag.Value = "Nobody won!"
			break
		elseif i == 0 then
			-- timer ran out
			statusTag.Value = "Times up!"
			break
		end
		task.wait(1)
		statusTag.Value = "There are " .. i .. " seconds remaining, and " .. #CollectionService:GetTagged("Alive") .. " players left"
	end

	print("END OF GAME")

	-- Clean up
	for _, player in pairs(activePlayers) do
		if CollectionService:HasTag(player, "Alive") then
			CollectionService:RemoveTag(player, "Alive")
		end
		if player.Backpack:FindFirstChild("Sword") then
			player.Backpack.Sword:Destroy()
		end
		if player.Character:FindFirstChild("Sword") then
			player.Character.Sword:Destroy()
		end
		player:LoadCharacter()
	end

	map:Destroy()
	statusTag.Value = "Game over"
	task.wait(2)
end

Here is what is in my game to make it a bit less confusing.

image

Thank you.

1 Like

It seems you aren’t propererly choosing a random map, so try the code below and whatever map is returned, clone and parent it.

function SelectMap()
	local Maps = {}

	for i, v in pairs(ServerStorage.Maps:GetChildren()) do
		if v:IsA("Model") then
			table.insert(Maps, v.Name)
		end
	end

	local randomMap = math.random(1, #Maps)
	local Map = Maps[randomMap]

	return Map
end
1 Like

do i just replace that current line with the script you wrote?

No, Add the function in but you will need to call it like so:

local ChosenMap = SelectMap()
game.ServerStorage.Maps:FindFirstChild(ChosenMap):Clone().Parent = game.Workspace

what do you mean? sorry im a begginer scripter i dont really know what im doing

Replace those lines you had with the following, you will need to make sure the path to the folders aligns with yours and change the code as needed:

function SelectMap()
	local Maps = {}

	for i, v in pairs(ServerStorage.Maps:GetChildren()) do
		if v:IsA("Model") then
			table.insert(Maps, v.Name)
		end
	end

	local randomMap = math.random(1, #Maps)
	local Map = Maps[randomMap]

	return Map
end

local ChosenMap = SelectMap()
game.ServerStorage.Maps:FindFirstChild(ChosenMap):Clone().Parent = game.Workspace

i put the code in but now these words are underlined
image

You don’t need local map and the lines under it. Be sure that ServerStorage has a folder name Maps with all the maps you have.

so can i just delete that line?

Yes, delete the local map and map.Parent lines

it came up with this error
image
image

change map.Name to ChosenMap, that’s all you need.

oh okay, does this script look good to you now?

It just needs to be ChosenMap, not ChosenMap.Name because the ChosenMap returns the name of the map.

it came up with this error


image

ChosenMap is the name of the map, you need to create a variable to identify the map object like so:

local Map = game.ServerStorage.Maps:FindFirstChild(ChosenMap)

i assume i just put that variable at the top of the script

Put the variable after ChosenMap is decided.

what do you mean by decided?
sorry if im being stupid

You’re fine, put it after local ChosenMap