I am trying to spawn maps into workspace(one at a time) in a loop for when players join. Then they teleport to it. It works, though it is only supposed to be one map. After they spawn in, another map starts loading in. Then the players teleport to that map. I have 2 maps in ServerStorage. FE is off.
Server Script is in ServerScriptService
maps = game.ServerStorage:GetChildren() while wait(.5) do if game.Players.NumPlayers >= 2 then for i, v in pairs(game.Workspace:GetChildren()) do if v:IsA('Hint') then v:Destroy() end end ranmap = math.random(1, #maps) wait(1) chosen = maps[ranmap] chosenclone = chosen:Clone() chosenclone.Parent = game.Workspace if chosenclone.Parent == game.Workspace then game.StarterGui.ScreenGui.Frame.Visible = true game.StarterGui.ScreenGui.Frame.Map.Visible = true game.StarterGui.ScreenGui.Frame.Madeby.Visible = true game.StarterGui.ScreenGui.Frame.MapName.Visible = true end wait(5) spawn = chosenclone.SpawnPoint wait(5) local player = game.Players.LocalPlayer for i,v in pairs(game.Players:GetPlayers()) do name = v.Name check = v.Character if check then checkHumanoid = check:FindFirstChild("Humanoid") if checkHumanoid then check:MoveTo(spawn.Position) end end end else if not(game.Workspace:findFirstChild("Hint")) then -- If a hint is not a child of the workspace h = Instance.new("Hint", game.Workspace) h.Text = "You need more than 1 player to play." end end end
You are spawning a new map every time the loop iterates when there are 2 or more players in the game. In order to prevent this from happening, ensure that a map has not already been instantiated.
Also, your code would benefit from using the Players.PlayerAdded
event.
local mapInstantiated = false game.Players.PlayerAdded:connect(function (player) --fired whenever a player joins if game.Players.NumPlayers >= 2 and mapInstantiated ~= true then --check whether there are enough players and also whether a map has been instantiated --do stuff mapInstantiated = true end end)