Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Map spawning loop fix?

Asked by
Spoookd 32
8 years ago

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

1 answer

Log in to vote
0
Answered by 8 years ago

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.PlayerAddedevent.

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)
0
I kept the whole script but removed the stuff you removed and put it the playeradded and mapinstanited and stuff like that, but then the hint that I put up if less than two people are in it will show a hint saying you need more than two players, even though I put two players in it stayed up. Spoookd 32 — 8y
Ad

Answer this question