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

How to make a script that teleports two random players?

Asked by 3 years ago

So, I'm extremely new with scripting and I've managed to create an elevator game. (Somehow) I'm trying to create a script that when the elevator doors open it'll randomly choose two players to enter the floor and before the time for the elevator is up, it'll teleport them back onto the elevator. I could really use some help on how to go about doing this because I have no idea where to even start. I'm not looking for someone to do it for me, but to help me grasp an understanding on how to go about writing it. Any tutorials you may have in your archive will help tremendously!

2 answers

Log in to vote
2
Answered by
Soban06 410 Moderation Voter
3 years ago
Edited 3 years ago

Since you haven't posted any script regarding what you have done, I will tell you how to select any random 2 players from the game.

So here it is:

wait(3) -- We add this wait to make sure the script loads after the player has loaded
warn("Script started")
local PlayersInGame = {}

for i, player in pairs(game.Players:GetPlayers()) do
    if player then
        table.insert(PlayersInGame, player)
        -- We insert all the players which are in the game into the table
    end
end

-- We create this function to see if that player has already been seelcted so we don't select him again'
local function CheckIfPlayerIsInTable(tab, name)
    local found = false
    for i, v in pairs(tab) do
        if v == name then
            found = true
        end
    end
    return found
end

-- We create an empty table. We will put the 2 chosen players in this table.
local function ChoosePlayers()
    local ChosenPlayers = {}
    repeat
        local SelectedPlayer = PlayersInGame[math.random(1, #PlayersInGame)]

        if CheckIfPlayerIsInTable(ChosenPlayers, SelectedPlayer) then
        else
            table.insert(ChosenPlayers, SelectedPlayer)
            -- If they already don't exist in the table then we add then using table.insert()
        end

    until
    #ChosenPlayers >= 1 -- Change '1' to how many players you want to be selected randomly
    return ChosenPlayers -- Then we return the Players which were chosen
end

local Results = ChoosePlayers()
for i, v in pairs(Results) do
    print(v)
    -- Now we loop through the Chosen Players and print it. 'v' basically means each player who was chosen.
end

Hope this helps. If you have any questions regarding this answer feel free to comment below.

0
Couldn't upload scripts at the time, but thank you for the script I appreciate it. I actually should've made it clearer in my post that I wasn't looking for a request. I was hoping that some users would have some helpful videos on how to script and how to go about understanding how to make this. My apologies for the misinformation on my behalf! I'll learn a lot from the scripts given. Thank you. ScarlettPhoenixx 2 — 3y
0
You're Welcome. If this answer helped you, consider accepting. Soban06 410 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

this is not a request site so next time please come with a script for us to fix/help you with.

game.Players.PlayerAdded:Connect(function()
    if #game.Players:GetPlayers() > 5 then -- checks for at least 5 players before game picks 2 random players (can remove this, just here to show you that its picking 2 random players or i recomend changing it to 2 players so it doesn't error)

        local playerList = game.Players:GetPlayers()
        local chosenPlayer1 = playerList[math.random(1, #playerList)]
        table.remove(playerList, table.find(playerList, chosenPlayer1))

        local chosenPlayer2 = playerList[math.random(1, #playerList)]
        table.remove(playerList, table.find(playerList, chosenPlayer2))
    end
end)
0
Couldn't upload scripts at the time, but thank you for posting, I appreciate it. I wasn't asking for a request. I said I didn't want others to do it for me, but to offer tutorials that can help understand how to script in order to go about making this. I should have made that clearer in my post, my apologizes ScarlettPhoenixx 2 — 3y

Answer this question