Issue with Randomly Loading Maps

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a game with randomly picked maps. I’m making it use a module script to pick a map, then return the map to a server script. The server script teleports the player to the map, and fires a remote event to a LocalScript which loads the map on the client.

  2. What is the issue? Include screenshots / videos if possible!
    The map simply isn’t loading. My prints come out fine, showing the spawnpoint, map and object name. But when teleported, i fall into the void.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I asked ChatGPT, but as usual it is bad a luau, and nothing worked.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

ServerScript

local MapModule = require(game:GetService("ServerScriptService").MapModule)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function typeText(text, Object, Interval, lifetime)
	for i = 1, #text do
		Object.Text = string.sub(text, 1, i)
		task.wait(Interval)
	end
	task.wait(lifetime) -- How long before the text disappears
	Object.Text = ""
end

script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
	local playerGui = plr:FindFirstChild("PlayerGui")
	if not playerGui then return end

	-- Create UI
	local screenGui = Instance.new("ScreenGui")
	screenGui.IgnoreGuiInset = true
	screenGui.Parent = playerGui

	local frame = Instance.new("Frame")
	frame.Size = UDim2.new(1, 0, 1, 0) -- Fullscreen fade
	frame.Position = UDim2.new(0, 0, 0, 0)
	frame.BackgroundTransparency = 1
	frame.BackgroundColor3 = Color3.new(0, 0, 0)
	frame.Parent = screenGui

	local TextLabel = Instance.new("TextLabel")
	TextLabel.Parent = frame
	TextLabel.Size = UDim2.new(0.5, 0, 0.1, 0)
	TextLabel.Position = UDim2.new(0.25, 0, 0.4, 0)
	TextLabel.BackgroundTransparency = 1
	TextLabel.TextColor3 = Color3.new(1, 1, 1)
	TextLabel.Text = "Loading..."
	TextLabel.TextScaled = true
	TextLabel.TextStrokeTransparency = 0
	TextLabel.TextWrapped = true
	TextLabel.Font = Enum.Font.Ubuntu
	task.wait(1)

	-- Tween animation
	local tweenService = game:GetService("TweenService")
	local fadeIn = tweenService:Create(frame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {BackgroundTransparency = 0})
	local fadeOut = tweenService:Create(frame, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {BackgroundTransparency = 1})

	-- Fade in
	fadeIn:Play()
	task.wait(1.5)

	-- Ensure character is valid before teleporting
	if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
		local Map = MapModule:PickMap()

		-- Debugging output
		print("Picked Map:", Map and Map.Name)
		print("Map Object:", Map and Map.Object)
		print("Map SpawnPoint:", Map and Map.SpawnPoint)

		if Map and Map.Object and Map.SpawnPoint then
			typeText(Map.Name, TextLabel, 0.1, 1.5)

			-- Move map to ReplicatedStorage to ensure client can access it
			Map.Object.Parent = ReplicatedStorage
			Map.Object:SetAttribute("IsMap", true)

			-- Fire client event with the map name (not the table)
			script.Parent.LoadMap:FireClient(plr, Map.Name)

			-- Teleport player
			local hrp = plr.Character.HumanoidRootPart
			hrp.CFrame = Map.SpawnPoint.CFrame
			plr.Character:FindFirstChildOfClass("Humanoid").Jump = true
		else
			warn("Invalid Map: Map.Object or Map.SpawnPoint is missing!")
		end
	end
	task.wait(0.5)

	-- Fade out
	fadeOut:Play()
	fadeOut.Completed:Wait() -- Ensures fade out finishes before destruction

	screenGui:Destroy()
end)

LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.LoadMap.OnClientEvent:Connect(function(mapName)
	print("🔥 Client received map name:", mapName)

	local mapObject = ReplicatedStorage:FindFirstChild(mapName)
	print("🧐 Found map object in ReplicatedStorage:", mapObject)

	if mapObject then
		-- Remove any previously loaded maps
		for _, v in pairs(workspace:GetChildren()) do
			if v:IsA("Model") and v:GetAttribute("IsMap") then
				v:Destroy()
			end
		end

		-- Move the map into the workspace
		mapObject.Parent = workspace
		print("✅ Map successfully moved to workspace!")
	else
		warn("⚠️ Map not found in ReplicatedStorage!")
	end
end)

ModuleScript

--// Services \\--
local RepStore = game:GetService("ReplicatedStorage")

--// Folders \\--
local MapFolder = RepStore:WaitForChild("Maps")

local MapModule = {}
local Maps = {
	FracturedCrossroads = {
		Name = "Fractured Crossroads",
		Object = MapFolder:WaitForChild("Crossroad"),
		SpawnPoint = MapFolder.Crossroad:WaitForChild("SpawnPoint")
	}
}

function MapModule:PickMap()
	-- Extract keys into an array
	local mapKeys = {}
	for key in pairs(Maps) do
		table.insert(mapKeys, key)
	end

	-- Pick a random key
	local RandomIndex = math.random(1, #mapKeys)
	local PickedMap = Maps[mapKeys[RandomIndex]]

	print("Picked Map:", PickedMap.Name)
	return PickedMap
end

return MapModule

I would appreciate any help :slight_smile:

You did not state an issue. You just described what you’re currently doing. Regardless, you should not be teleporting players to a map that hasn’t loaded yet. Instead of having the clients load the maps, load the map from the server, then teleport the players

it’s a local script because each player gets a different map, ill edit the post now.

Do not teleport the player to the map until it has finished loading