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!
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.
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)