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

How can I make players spawn to different spawnlocations?

Asked by 4 years ago

How can I make this so all players will be splited and teleported to different spawnpoints?

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
                if plr.Character then
                    plr.Character.HumanoidRootPart.CFrame = spawnpoint.CFrame or spawnpoint2.CFrame or spawnpoint3.CFrame
                    print("All players have been teleported to: "..spawnpoint.Name)
0
Use math.random to randomly select a spawnpoint. DevingDev 346 — 4y

2 answers

Log in to vote
0
Answered by
DevingDev 346 Moderation Voter
4 years ago
Edited 4 years ago

Explanation

You seemed to have misunderstood how or works; I'll explain it for you real quick.

In your situation or means:

if spawnpoint1 doesn't exist then choose spawnpoint 2. If spawnpoint2 doesn't exist then choose spawnpoint3.

But I'll assume these 3 spawnpoints will always exist in your game throughout the whole game. Thus using or will not work here.

Fix

Math.random fixes this problem and randomly selects a spawnpoint, and then teleports the player to it.

Implementation

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
    if player.Character then
        local character = player.Character

        local index = math.random(1, #workspace.spawnpoints:GetChildren())
        local spawnPoint = workspace.spawnpoints:GetChildren()[index]

        character.HumanoidRootPart.CFrame = spawnPoint.CFrame
        print("All players have been teleported to: "..spawnpoint.Name)
    end
end
0
Hey, I have send a question but I didn't reply to this because I can't type lua code in here. Hope you get a notification now :) GasPotion 7 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Alright, Thank you for you're help! I suppose that the "spawnpoints" is a folder wich the GetChildren all the things inside gets? However, I get a Infinite yield possible on 'Workspace.Obby:WaitForChild("Spawn")'.

I have changed the location a little bit to where the spawns are located, But it does not work?

The spawns are located at: game.Workspace.Obby.Spawns

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        if player.Character then
            local character = player.Character

            local index = math.random(1, #workspace.Obby.Spawns:GetChildren())
            local spawnPoint = workspace.Obby.Spawns:GetChildren()[index]

            character.HumanoidRootPart.CFrame = spawnPoint.CFrame
            print("All players have been teleported to: "..spawnPoint.Name)
        end
    end

Can you help me again with this?

Thanks in advice.

Answer this question