Random Map generation spawn a map per player

  1. What do you want to achieve?
    I Made a script not so long ago, and i have problems with it. I would like to have the script working.

  2. What is the issue?
    When i start the game in test mode or in normal game with more then 1 player, the script generates a map per player. The problem seems correlated to defining the player wrong.

This is my script:

local player = game.Players


	

function Return()
	print("Start Return() function")
	
	
	
print(player)
wait(2)
	print("Wait 2")
function RandomMap()
	print("Function RandomMap")
	RandomNum = math.random(1, 8)
end
game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
	game:GetService("ReplicatedStorage").Events.GetGUI:FireClient(playerr)
end)
	print("GetGUI (countdown) fireclient")	
	wait(15)
	print("wait 15")
		RandomMap()
		print("run RandomMap()")
print("RandomNUM:", RandomNum)

local SurfaceGUIs = game.Workspace.Lobby.SurfaceGUIs
local MapImage = SurfaceGUIs.Parent.MapImage
		local Lightning = game.Lighting
		print("some localz")
	
	
------------------------------------------------------------------------------|
if RandomNum == 1 then
				local GUIclone = SurfaceGUIs.one:Clone()
				GUIclone.Parent = MapImage
				local MAPclone = Lightning.one:Clone()
				MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
			end)
						
				game.Workspace.Playing.Value = true
		wait(350)
		
				local Sound = game:GetService("SoundService").TimeUP
				local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
				Event:FireClient(player)

						Sound.Playing = true
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)
				for i, player in ipairs(game.Players:GetPlayers()) do
					if player.Character then
						local hum = player.Character:FindFirstChild('Humanoid')
						if hum then
							hum.Health = 0
						end	
					end
		end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
				game.Workspace[player.Name][player.Name]
		end
				game.Workspace.Playing.Value = false
				GUIclone:Destroy()
				MAPclone:Destroy()
				wait(10)
				Return()
end -- of if
	
if RandomNum == 2 then
			local GUIclone = SurfaceGUIs.two:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.two:Clone()
			MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)
			
			game.Workspace.Playing.Value = true
						
			wait(350)
			local Sound = game:GetService("SoundService").TimeUP
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
		Event:FireClient(player)

			Sound.Playing = true
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)
					
			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
		end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
				game.Workspace[player.Name][player.Name]
		end
				game.Workspace.Playing.Value = false
				print("player killed")
				GUIclone:Destroy()
				MAPclone:Destroy()
				print("part Destroyed")
				wait(10)
				Return()
				print("script returned")
			end -- of if
				
if RandomNum == 3 then
			local GUIclone = SurfaceGUIs.three:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.three:Clone()
			MAPclone.Parent = game.Workspace.Maps
			game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
			end)
			game.Workspace.Playing.Value = true
			
			wait(350)
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
		Event:FireClient(player)

		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)

			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
		end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)

			game.Workspace.Playing.Value = false
			print("player killed")
			GUIclone:Destroy()
			MAPclone:Destroy()
			print("part Destroyed")
			wait(10)
			Return()
			print("script returned")
end -- of if

if RandomNum == 4 then
			
			local GUIclone = SurfaceGUIs.four:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.four:Clone()
			MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)
			game.Workspace.Playing.Value = true

			wait(350)
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
		Event:FireClient(player)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)

			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
		end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
				game.Workspace[player.Name][player.Name]
		end
			game.Workspace.Playing.Value = false
			print("player killed")
			GUIclone:Destroy()
			MAPclone:Destroy()
			print("part Destroyed")
			wait(10)
			Return()
			print("script returned")
			
end -- of if
		
if RandomNum == 5 then
			
			local GUIclone = SurfaceGUIs.five:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.five:Clone()
			MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)
			game.Workspace.Playing.Value = true

			wait(350)
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireClient(player)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)
			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
			end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
			if player.Character:FindFirstChild("Webley Mk. IV") then
				player.Character:FindFirstChild("Webley Mk. IV").Parent =
					game.Workspace[player.Name][player.Name]
			end
		
			game.Workspace.Playing.Value = false
			print("player killed")
			GUIclone:Destroy()
			MAPclone:Destroy()
			print("part Destroyed")
			wait(10)
			Return()
			print("script returned")
			
end -- of if
		
if RandomNum == 6 then
			
			local GUIclone = SurfaceGUIs.six:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.six:Clone()
			MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)
			game.Workspace.Playing.Value = true

			wait(350)
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireClient(player)
		
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)

			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
		end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
				game.Workspace[player.Name][player.Name]
		end
			game.Workspace.Playing.Value = false
			print("player killed")
			GUIclone:Destroy()
			MAPclone:Destroy()
			print("part Destroyed")
			wait(10)
			Return()
			print("script returned")
			
end -- of if
		
if RandomNum == 7 then
			
			local GUIclone = SurfaceGUIs.seven:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.seven:Clone()
			MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)
			game.Workspace.Playing.Value = true

			wait(350)
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireClient(player)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)

			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
		end
		
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
				game.Workspace[player.Name][player.Name]
		end
			game.Workspace.Playing.Value = false
			print("player killed")
			GUIclone:Destroy()
			MAPclone:Destroy()
			print("part Destroyed")
			wait(10)
			Return()
			print("script returned")
			
end -- of if
		
if RandomNum == 8 then
			
			local GUIclone = SurfaceGUIs.eight:Clone()
			GUIclone.Parent = MapImage
			local MAPclone = Lightning.eight:Clone()
			MAPclone.Parent = game.Workspace.Maps
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)
			game.Workspace.Playing.Value = true

			wait(350)
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireClient(player)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(player)

		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)

			for i, player in ipairs(game.Players:GetPlayers()) do
				if player.Character then
					local hum = player.Character:FindFirstChild('Humanoid')
					if hum then
						hum.Health = 0
					end	
				end
		end
		wait(2.5)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)
		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
				game.Workspace[player.Name][player.Name]
		end
			game.Workspace.Playing.Value = false
			print("player killed")
			GUIclone:Destroy()
			MAPclone:Destroy()
			print("part Destroyed")
			wait(10)
			Return()
			print("script returned")
			
end -- of if

------------------------------------------------------------------------------|

	end-- of Return()
	Return()

This is the last version i tried to write, In this version i get the player from a RemoteEvent

P.S. I’m new with Devhub and roblox studio in general.

You have a bananas amount of repeated code that you should sort out first, the code used to load the map is repeated for every possible map. You can put the code in a function like this:

local mapsByNumber = {
	4 = "four",
	5 = "five",
	--etc
}

local function LoadMap(mapNumber)
		local mapName = mapsByNumber[mapNumber]
		local GUIclone = SurfaceGUIs[mapName]:Clone()
		GUIclone.Parent = MapImage
		local MAPclone = Lightning[mapName]:Clone()
		MAPclone.Parent = game.Workspace.Maps

		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Playing
		end)

		game.Workspace.Playing.Value = true

		wait(350)

		local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
		Event:FireClient(player)
		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)	
			playerr.Team = game.Teams.Lobby
		end)

		for i, player in ipairs(game.Players:GetPlayers()) do
			if player.Character then
				local hum = player.Character:FindFirstChild('Humanoid')
				if hum then
					hum.Health = 0
				end	
			end
		end

		wait(2.5)

		game:GetService("ReplicatedStorage").Events.GetPlayer.OnServerEvent:Connect(function(playerr)
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(playerr)
		end)

		if player.Character:FindFirstChild("Webley Mk. IV") then
			player.Character:FindFirstChild("Webley Mk. IV").Parent =
			game.Workspace[player.Name][player.Name]
		end

		game.Workspace.Playing.Value = false
		print("player killed")
		GUIclone:Destroy()
		MAPclone:Destroy()
		print("part Destroyed")
		wait(10)
		print("load map callback")
		--What you're doing here is called a callback
		--There is a built in optimization if you make sure this 
		--call is the last statement in the function
		Return()
end

--Load a map by random number
RandomNum = math.random(1, 8)
LoadMap(RandomNum)

Thank you for the code, i’m not so good in scripting…
By the way the local mapsByNumber gives me an error:

img

I think I solved it, instead of 1 = need 2 ==

you should put

local mapsByNumber = {
1 = "one",
2 = "two",
3 = "three",
4 = "four",
5 = "five",
6 = "six",
7 = "seven",
8 = "eight"
}

thats what they meant by etc
and if that gives you an error then put the name of the map you want for each one

1 Like

that will make it a bool value, don’t use ==
See kiann’s post thats what I meant. You could also change you map names to like “Map1” and get them like:

local MAPclone = Lightning["Map"..mapNumber]:Clone()

i Resolved doing:

		local mapsByNumber = {
			[1] = "one",
			[2] = "two",
			[3] = "three",
			[4] = "four",
			[5] = "five",
			[6] = "six",
			[7] = "seven",
			[8] = "eight"
		}

it works now?

chat limit char limit