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

Multiple teleport locations how to make every player go to a different location?

Asked by 3 years ago
Edited 3 years ago

I have made it so i teleport a bunch of players to different locations, but sometimes they go to the same location. I want to assign every teleporting player a different location to teleport to all random. So if i have 8 players and 8 locations, every player has his own location. As if right now i can get random teleports but they can spawn on the same location. I have used the following code:

``

local lobbyLocation = game.Workspace.Lobby.Lobby.Position + Vector3.new(0,3,0)


local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')



local function playGame()

    local timeAmount = 20

    local timerText = 'Remaining Time: '

    while timeAmount > 0 do

        timeEvent:FireAllClients(timeAmount, timerText)

        wait(1)

        timeAmount -= 1

    end

end



local function playIntermission()

    local intermission = 10

    local timerText = 'Intermission: '

    while intermission > 0 do

        timeEvent:FireAllClients(intermission, timerText)

        wait(1)

        intermission -= 1

    end

end



local function resetPlayers()

    for _, plr in pairs(game.Players:GetChildren()) do

        plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)

    end 

end



local function teleportPlayers()    
        local plrs = game.Players:GetChildren()
        for i = 1, #plrs do
            local num = math.random(1,9)
            plrs[i].Character.Head.CFrame =                                      
CFrame.new(workspace.Teleports["Main"..num].Position)

    end 

end



while true do

    resetPlayers()

    playIntermission()

    teleportPlayers()

    playGame()

end
0
Why not try Teams and make it so 1 player max in each one? The spawn has a team assigned to it. KidMudasir7 15 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Ok, so the problem is that when you get a random value using math.random() it sometimes can repeat the same numbers.

So I made a script that allows you to get a random value without it repeating itself

Basically, it gets a random number, check if it exists in a table and if not it will return it and return true and put it in the table and if it exists in the table it will return nil and return false. With this you can use the repeat to loop it until it gets true.

local function GetRandomValue(min, max, tableList)
    local number = math.random(min,max)
    local success = true
    for i, v in ipairs(tableList) do
        if v == number then
            success = false
        end
    end
    if success then
        table.insert(tableList, number)
        return number, true
    elseif not success then
        return nil, false
    end
end

local function teleportPlayers()
    local plrs = game.Players:GetChildren()
    local value = {}
    for i = 1, #plrs do
        local num, success
        repeat 
        num, success = GetRandomValue(1, 9, value)
        until success
        plrs[i].Character.Head.CFrame =                                     
        CFrame.new(workspace.Teleports["Main"..num].Position)
    end
end

I hope this helps.

0
It worked perfectly dude thank u so much!!!! DrBeCool 2 — 3y
0
Yeah this is extremely excessive, see my approach below. Ziffixture 6913 — 3y
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago
local function TeleportPlayers()
    local PlayersToTeleport = Players:GetPlayers()
    local Teleports = Teleports:GetChildren()
    ---------------
    if (#PlayersToTeleport <= #Teleports) then
        ---------------
        for _, Player in ipairs(PlayersToTeleport) do
            ---------------
            local Teleport = table.remove(Teleports, math.random(#Teleports))
            ---------------
            if (Player.Character) then
                ---------------
                local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
                if (Humanoid and Humanoid.Health > 0) then
                    ---------------
                    Humanoid.RootPart.CFrame = CFrame.new(
                        Teleport.Position + Vector3.new(0, 3.5, 0)
                    )
                end
            end
        end
    else
        error(
            "Player-teleport ratio unbalanced; not enough teleports to evenly distribute."
        )
    end
end
0
Nice, I never thought of it that way realchricrocgamer 15 — 3y

Answer this question