Something wrong in map generation script

Goodmorning, i have a problem with my random map generation script.
I will try to explain myself the best:

I took this script from devforum but i still have a major problem, the script is composed with a main function called Return(), This function in included in a game.Players.PlayerAdded:Connect(function(player) function.

What is the issue?
The script generates a random number from 1 to 8 for the maps like this:

local RandomNum = math.random(1, 8)

Then, there is the LoadMap() function that executes all the tasks nedeed for the game.

Now the Problem:
The script that i will include under, spawn in the maps folder, a map per player.
I think this error is correlated to a wrong way to define the player.

I tried all i could believe, please help :grinning:

SCRIPT:

game.Players.PlayerAdded:Connect(function(player)
local function Return()
	
wait(2)
	
	
		game:GetService("ReplicatedStorage").Events.GetGUI:FireAllClients()	
		wait(15)
	
	
		--Load a map by random number
		local RandomNum = math.random(1, 8)
		
		local mapsByNumber = {
			[1] = "one",
			[2] = "two",
			[3] = "three",
			[4] = "four",
			[5] = "five",
			[6] = "six",
			[7] = "seven",
			[8] = "eight"
		}
		
		local function LoadMap(mapNumber)

			local SurfaceGUIs = game.Workspace.Lobby.SurfaceGUIs
			local MapImage = SurfaceGUIs.Parent.MapImage
			local Lightning = game.Lighting

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

			game.Workspace.Playing.Value = true

			wait(30)
		
		
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireAllClients()
		
		
				player.Team = game.Teams.Lobby
		

			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)

		
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(player)
		
			

			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(5)
		Return()
			print("load map callback")

		end -- of LoadMap()
		
		LoadMap(RandomNum)
	
end -- of Return()
Return()
end) -- player

It’s quite simple. Instead of the Function just write it outside. It will run everytime the server starts rather than when the player joins.

So

--Your code

game.Players.PlayerAdded:Connect(function(player))
local function Return()
local function Return()
	
wait(2)
	
	
		game:GetService("ReplicatedStorage").Events.GetGUI:FireAllClients()	
		wait(15)
	
	
		--Load a map by random number
		local RandomNum = math.random(1, 8)
		
		local mapsByNumber = {
			[1] = "one",
			[2] = "two",
			[3] = "three",
			[4] = "four",
			[5] = "five",
			[6] = "six",
			[7] = "seven",
			[8] = "eight"
		}
		
		local function LoadMap(mapNumber)

			local SurfaceGUIs = game.Workspace.Lobby.SurfaceGUIs
			local MapImage = SurfaceGUIs.Parent.MapImage
			local Lightning = game.Lighting

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

			game.Workspace.Playing.Value = true

			wait(30)
		
		
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireAllClients()
		
		
				player.Team = game.Teams.Lobby
		

			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)

		
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(player)
		
			

			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(5)
		Return()
			print("load map callback")

		end -- of LoadMap()
		
		LoadMap(RandomNum)
	
end -- of Return()
Return()
end
end

Should be changed to

if #game.Players:GetChildren() > 0
local function Return()
	
wait(2)
	
	
		game:GetService("ReplicatedStorage").Events.GetGUI:FireAllClients()	
		wait(15)
	
	
		--Load a map by random number
		local RandomNum = math.random(1, 8)
		
		local mapsByNumber = {
			[1] = "one",
			[2] = "two",
			[3] = "three",
			[4] = "four",
			[5] = "five",
			[6] = "six",
			[7] = "seven",
			[8] = "eight"
		}
		
		local function LoadMap(mapNumber)

			local SurfaceGUIs = game.Workspace.Lobby.SurfaceGUIs
			local MapImage = SurfaceGUIs.Parent.MapImage
			local Lightning = game.Lighting

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

			game.Workspace.Playing.Value = true

			wait(30)
		
		
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireAllClients()
		
		
				player.Team = game.Teams.Lobby
		

			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)

		
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(player)
		
			

			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(5)
		Return()
			print("load map callback")

		end -- of LoadMap()
		
		LoadMap(RandomNum)
	
end -- of Return()
Return()
end) -- if loop

what should I write instead of “Other Stuff that isn’t to be messed with”

Oh yeah sorry I was in a hurry. It’s basically rest of the script excluding the end i put in the end

so all the Return() function right?

1 Like

You put the Return() function inside the .PlayerAdded event function, meaning that every time a player joins the game, it will call the Return() function.

You can put the Return() function outside of the event function to solve the issue.

1 Like

I put Return() function inside because player function connect (player), if i put return out, it will not recognise the player

in the second part, where do i get the player?

I will reply in a bit im reading ur code.

I changed the script with this one, it resolved the map per player problem but created a problem relative to this line:

SCRIPT (new):

game.Players.PlayerAdded:Connect(function(plr)
	player = plr
end) -- player

local function Return()
	
wait(2)
	
	
		game:GetService("ReplicatedStorage").Events.GetGUI:FireAllClients()	
	wait(15)
	print("waited 15 sec")
	
	
		--Load a map by random number
		local RandomNum = math.random(1, 8)
		
		local mapsByNumber = {
			[1] = "one",
			[2] = "two",
			[3] = "three",
			[4] = "four",
			[5] = "five",
			[6] = "six",
			[7] = "seven",
			[8] = "eight"
		}
		
		local function LoadMap(mapNumber)

			local SurfaceGUIs = game.Workspace.Lobby.SurfaceGUIs
			local MapImage = SurfaceGUIs.Parent.MapImage
			local Lightning = game.Lighting

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

			game.Workspace.Playing.Value = true

			wait(30)
		
		
			local Event = game:GetService("ReplicatedStorage").Events.GetGUI2
			Event:FireAllClients()
		
		
				player.Team = game.Teams.Lobby
		

			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)

		
			local GunEvent = game:GetService("ReplicatedStorage").Events.FromBPtoFolder
			GunEvent:FireClient(player)
		
			

			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(5)
		Return()
			print("load map callback")

		end -- of LoadMap()
		
		LoadMap(RandomNum)
	
end -- of Return()
Return()

Line that it seems to be skipped:

game:GetService("ReplicatedStorage").Events.GetGUI:FireAllClients()	
wait(15)

Also you can see, in the new script after the line, there is a print("waited 15 sec")
It prints this in server output.

1 Like

Are you listening to the event on the client (kinda obvious but still considered to ask it). If yes then add a print to see if it recieves the fired event.

I solved it, the problem was that I used the 2-player TEST mode, and since the action starts when the server starts, by the time it loaded the players and everything, the gui action had already been executed. Thanks for the help

1 Like