Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with spawning to random spawns?

        gspawn = chosenMap.GreenSpawns:GetChildren()
        yspawn = chosenMap.YellowSpawns:GetChildren()

        for i,v in pairs(game.Players:GetPlayers()) do 
            name = v.Name
            check = game.Workspace:FindFirstChild(name)
            if v.TeamColor == BrickColor.new("Lime green") then
                if check then
                    checkHumanoid = check:FindFirstChild("Humanoid")
                    if checkHumanoid then
                        local distance = math.random(-4, 4);
                        check:MoveTo(gspawn.Position + Vector3.new(distance, distance, distance))
                    	v.Team.Value = "Lime green"
					end
                end
            elseif v.TeamColor == BrickColor.new("New Yeller") then
                if check then
                    checkHumanoid = check:FindFirstChild("Humanoid")
                    if checkHumanoid then
                        local distance = math.random(-4, 4);
                        check:MoveTo(yspawn.Position + Vector3.new(distance, distance, distance))
                    	v.Team.Value = "New Yeller"
					end
                end
            end
        end

The GreenSpawns and YellowSpawns are models inside 'chosenMap' and their children are spawn models. There is a problem. 1. The players dont teleport to the spawns, and if there arent models then every player spawns to one brick. Please help. I just want it to get the spawn models in the game and teleport every player to the game to a random spawn that corresponds to the players team.

1 answer

✓ Accepted answer

Your problem is that gspawn and yspawn are Tables, not specific spawn locations.

A secondary problem that your code will have is that players can spawn inside the ground - usually ending with them falling to death.

I've modified your teleportation to fix both problems:

gspawn = chosenMap.GreenSpawns:GetChildren()
yspawn = chosenMap.YellowSpawns:GetChildren()

for i,v in pairs(game.Players:GetPlayers()) do 
    name = v.Name
    check = game.Workspace:FindFirstChild(name)
    if v.TeamColor == BrickColor.new("Lime green") then
        if check then
            checkHumanoid = check:FindFirstChild("Humanoid")
            if checkHumanoid then
                local distance = math.random(-4, 4);
                check:MoveTo(gspawn[math.random(#gspawn)].Position + Vector3.new(distance, 4, distance))
                v.Team.Value = "Lime green"
            end
        end
    elseif v.TeamColor == BrickColor.new("New Yeller") then
        if check then
            checkHumanoid = check:FindFirstChild("Humanoid")
            if checkHumanoid then
                local distance = math.random(-4, 4);
                check:MoveTo(yspawn[math.random(#yspawn)].Position + Vector3.new(distance, 4, distance))
                v.Team.Value = "New Yeller"
            end
        end
    end
end

And here is a more compact version of your code, with the sanity check rewritten:

gspawn = chosenMap.GreenSpawns:GetChildren()
yspawn = chosenMap.YellowSpawns:GetChildren()

for i,v in pairs(game.Players:GetPlayers()) do 
    name = v.Name
    if v.Character then
    v.Team.Value = (v.TeamColor == BrickColor.new("New Yeller")) and "New Yeller" or "Lime green"
    spawn = (v.Team.Value == "New Yeller") and yspawn or gspawn 
            if v.Character:FindFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 then
                local distance = math.random(-4, 4);
                v.Character:MoveTo(spawn[math.random(#spawn)].Position + Vector3.new(distance, 4, distance))
            end
    end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.