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

How do i make a map randomiser using this code? I have 10 maps so far btw

ive been having struggles randomising maps, everything i tried has gone wrong heres my code: (the bit where it selects the map is on line 102 but i put all of it cuz im new to scripting)

local minigameModule = {
	gameRunning = false,
	playersAlive = {},
	currentMap = nil
}

local waitForChild = game.WaitForChild
local findFirstChild = game.FindFirstChild

-- Modules

local settingsModule = require(waitForChild(script, "Settings"))
local onWin = (function()
	local onWinModule = findFirstChild(script, "OnWin")
	
	if onWinModule then
		local onWinFunction = require(onWinModule)
		
		if type(onWinFunction) == "function" then
			return onWinFunction
		end
	end
end)()

local remoteEvent = waitForChild(game:GetService("ReplicatedStorage"), "Event")
local mapsStorage = waitForChild(game:GetService("Lighting"), "Maps"):GetChildren(math.random)
local Maps = waitForChild(game:GetService("Lighting"), "Maps"):GetChildren()

local playersService = game:GetService("Players")

function minigameModule.isPotentialGame()
	return playersService.NumPlayers >= settingsModule.minimumPlayers 
end

function minigameModule:chooseMap()
	local chosenMap = mapsStorage[#mapsStorage]:Clone()
	if findFirstChild(chosenMap, "Spawns") then
		chosenMap.Parent = workspace
		chosenMap:MakeJoints()
		self.currentMap = chosenMap
		return chosenMap
	end
end

function minigameModule:spawnPlayers()
	local playersAlive = self.playersAlive
	local spawns = self.currentMap.Spawns:GetChildren()
	for index = 1, #playersAlive do
		local playerData = playersAlive[index]
		playerData.playerHumanoidRoot.CFrame = spawns[math.random(#spawns)].CFrame
	end
end

function minigameModule:runIntermission()
	if settingsModule.intermissionTime > 0 then
		for currentTime = math.floor(settingsModule.intermissionTime), 0, -1 do
			remoteEvent:FireAllClients("Timer", currentTime)
			wait(1)
		end
	end
	remoteEvent:FireAllClients("CeaseGUIs")
end

function minigameModule:getAlivePlayers()
	local playersAlive = {}
	for index, currentPlayer in next, playersService:GetPlayers() do
		local playerCharacter = currentPlayer.Character
		if playerCharacter then
			local playerHumanoidRoot = findFirstChild(playerCharacter, "HumanoidRootPart")
			local playerHumanoid = findFirstChild(playerCharacter, "Humanoid")
			if playerHumanoid and playerHumanoidRoot then
				table.insert(playersAlive, {
					player = currentPlayer,
					playerHumanoid = playerHumanoid,
					playerHumanoidRoot = playerHumanoidRoot
				})
			end
		end
	end
	return playersAlive
end

function minigameModule:isLegalGame()
	if #self:getAlivePlayers() >= settingsModule.minimumPlayers then
		return true
	end
end

function minigameModule:queryGameStart()
	if self.gameRunning then
		return
	elseif self.isPotentialGame() then
		self.gameRunning = true
		remoteEvent:FireAllClients("CeaseGUIs")
		self:runIntermission()
		
		if self:isLegalGame() then
			if settingsModule.roundDuration > 0 then
				
				local lastMap = 0
		
				local currentMap = self:chooseMap()
				local mapWeapons = findFirstChild(currentMap, "Weapons")
				local playersAlive = self:getAlivePlayers()
				self.playersAlive = playersAlive
				
				for index = 1, #playersAlive do
					local currentPlayer = playersAlive[index]
					local backpack = findFirstChild(currentPlayer.player, "Backpack")
					
					if backpack and mapWeapons then
						for index, weapon in next, mapWeapons:GetChildren() do
							weapon:Clone().Parent = backpack
						end
					end
					
					local connection
					connection = currentPlayer.playerHumanoid.Died:connect(function()
						connection:disconnect()
						table.remove(playersAlive, index)
						if #playersAlive < 2 then
							local winner = playersAlive[1]
							if winner then
								self:endGame(winner.player.Name .. " has won!", winner.player)
							else
								self:endGame("No one has won!")
							end
						end
					end)
				end
				
				if mapWeapons then
					mapWeapons:Destroy()
				end
				
				self:spawnPlayers()		
				
				remoteEvent:FireAllClients("Message", currentMap.Name .. " was chosen!", 5)
				
				for currentTime = settingsModule.roundDuration, 0, -1 do
					if not self.gameRunning then
						return
					end
					remoteEvent:FireAllClients("Timer", currentTime)
					wait(1)
				end
				self:endGame("The timer ran out! No one has won!")
			end
			
		else
			self:endGame("Not enough players alive to begin the round!")
		end
	else
		local remainingPlayers = settingsModule.minimumPlayers - playersService.NumPlayers
		remoteEvent:FireAllClients("Message", "Waiting for " .. remainingPlayers .. " player" .. (remainingPlayers > 1 and "s" or "") .. " to join.")
	end
end

function minigameModule:endGame(outputMessage, winner)
	if self.gameRunning then
		
		self.gameRunning = false
		self.currentMap:Destroy()
		self.currentMap = nil
		
		if winner and onWin then
			onWin(winner)
		end
		
		for index, player in next, playersService:GetPlayers() do
			player:LoadCharacter()
		end
		
		wait(1)
		
		remoteEvent:FireAllClients("Message", outputMessage, 5)
		
		wait(5)
		
		self:queryGameStart()
	end
end

function minigameModule:removePlayer(player)
	if self.gameRunning then
		for index = 1, #self.playersAlive do
			if self.playersAlive[index].player == player then
				table.remove(self.playersAlive, index)
				if #self.playersAlive <= 1 then
					self:endGame("Not enough players to continue the game.")
				end
				break
			end
		end	
	end
end

playersService.PlayerAdded:connect(function()
	minigameModule:queryGameStart()
end)

playersService.PlayerRemoving:connect(function(player)
	minigameModule:removePlayer(player)
end)

10 comments

0

Hi thanks for the comment can you add me to team create so i can investigate what is wrong?

0

ok i just did

0

no you didnt

0

Anyways I could see why it didnt work, sorry i forgot to cone the map can you now pls try to inset the code?

0

clone*

0

Hi I am so sorry for giving a script that does not work, I accidentally put in a wrong variable, could you please try now?, SO sorry!

0

ur helping me, you shouldnt be sorry. also it says in the dev consle cone is not a valid member of lighting.maps."name of map"

0

there are parts called "spawn" where the players are meant to teleport, in each map there is a folder called "spawns"

0

so the script doesnt teleport the players lol

0

Oh My God I am so sorry for all my mistakes! I have edited again the script!

1 answer

✓ Accepted answer

Well 1st of all, it looks like the map is being chosen at line 35. The script was extremely long but I do know a way to clone and parent a random map to workspace.

So im guessing you have a folder of maps, in lets say they were in lighting as written in the script.

So instead of this:

function minigameModule:chooseMap()
    local chosenMap = mapsStorage[#mapsStorage]:Clone()
    if findFirstChild(chosenMap, "Spawns") then
        chosenMap.Parent = workspace
        chosenMap:MakeJoints()
        self.currentMap = chosenMap
        return chosenMap
    end
end

you can do this:

function minigameModule:chooseMap()
    local randomnum = math.random(1,6)
    local chosenMap = mapsStorage[randomnum]:Clone()
    if findFirstChild(chosenMap, "Spawns") then
        chosenMap.Parent = workspace
        chosenMap:MakeJoints()
        self.currentMap = chosenMap
        return chosenMap
    end
end

I hope this helped.

Any questions? Just ask!

10 comments

0

thanks for replying!

0

thanks for replying! i pasted it in but once i went ahead and tested the game, the script completely broke. there are red lines under "chosen map" and "Map" and also the "." after mappick.parent

0

So many errors with this script. First of it should be "Clone" no "Cone", when getting a module script you must use return

0

When you call a function with a name like this function minigameModule:Choosemap() it will error out, unless you're using sometype of built-in-function like .self. .Self is part of metatables

0

you updated your code, but you're missing an end. 

0

oh thanks jesse but i dont know what metatables are really

0

Oh tanks!

0

Oh thanks!

0

Oh thanks!

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