Need help trying to make chosen map randomised and not the same map everytime

-- Services --

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local ServerStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")

------------------

-- GUI Elements --

local textLabel = StarterGui.ScreenGui:WaitForChild("TextLabel")

------------------

-- Remote Events --

local killPlayer = ReplicatedStorage:WaitForChild("killPlayer")
local intermission = ReplicatedStorage:WaitForChild("Intermission")
local teleportPlayers = ReplicatedStorage:WaitForChild("TeleportPlayers")

------------

-- Values --

local InGame = game.Workspace:WaitForChild("InGame")
local InLobby = game.Workspace:WaitForChild("InLobby")
local StringValue = game.Workspace:WaitForChild("Time")

------------
-- Lobby SpawnLocation --

local LobbySpawn = game.Workspace:WaitForChild("SpawnLocation")


-- Maps --

local MapsFolder = ServerStorage:WaitForChild("Maps")
local Maps = {MapsFolder:WaitForChild("Map1"), MapsFolder:WaitForChild("Map2", MapsFolder:WaitForChild("Map3"))}
local selectedMap = Maps[math.random(1, #Maps)]
local map = selectedMap:Clone()
-----------

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character) -- The parameter of the function will refer to the player's Character in this case because the activation of the CharacterAdded event sends the Character instance to the function
		while true do

			if InLobby.Value == true then
				Character.HumanoidRootPart.Position = Vector3.new(LobbySpawn.CFrame)
				for i = 5, 0, -1 do

					StringValue.Value = "Intermission: " .. i
					task.wait(1)
				end
				-- set these values after the for loop, do not need to check i == 0
				InLobby.Value = false
				InGame.Value = true 
			elseif InGame.Value == true then
				local char = Character		

				for _,player in pairs(game:GetService("Players"):GetPlayers())do
					if player and player.Character then
						local selectedLocation = selectedMap.SpawnLocations:GetChildren()[math.random(1, #selectedMap.SpawnLocations:GetChildren())] 
						
						map.Parent = workspace
						player.Character:MoveTo(selectedLocation.Position)
						
					end
				end
				for i = 5, 0, -1 do
					StringValue.Value = "Time remaining: " .. i
					task.wait(1)
				end
				
				
				map.Parent = game.ServerStorage
				InGame.Value = false
				InLobby.Value = true
			else
				error("Not in lobby or game!") -- errors help debugging
			end
		end
	end)
end)

Just stuck as I cannot get figure out how to make math.random reset as when the selectedMap is chosen it just stays the same

Any help is much appreciated thanks

math.random should reset whenever your game ends and restarts?

Whenever the function is called, it randomizes, how many maps do you have?

And do you ever call it?

I have 3 maps its in the table and I just called it in the variable it probs bad idea lol

Did you make a table of the maps?

local Maps = {MapsFolder:WaitForChild("Map1"), MapsFolder:WaitForChild("Map2", MapsFolder:WaitForChild("Map3"))}

I think the issue here is the value is not changing. Set selectedMap again each time a new map is picked

selectedMap = Maps[math.random(1, #Maps)]

if u dont, then selectedMap will always have the same value bc it only ran once at the beginning of the script.

2 Likes

Was thinking it was something like that but just got stuck on how to do it lol got it sorted now thank you very much helped me loads both of you cheers.

1 Like