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:
01 | wait( 3 ) -- We add this wait to make sure the script loads after the player has loaded |
02 | warn( "Script started" ) |
03 | local PlayersInGame = { } |
04 |
05 | for i, player in pairs (game.Players:GetPlayers()) do |
06 | if player then |
07 | table.insert(PlayersInGame, player) |
08 | -- We insert all the players which are in the game into the table |
09 | end |
10 | end |
11 |
12 | -- We create this function to see if that player has already been seelcted so we don't select him again' |
13 | local function CheckIfPlayerIsInTable(tab, name) |
14 | local found = false |
15 | for i, v in pairs (tab) do |
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.
01 | game.Players.PlayerAdded:Connect( function () |
02 | 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) |
03 |
04 | local playerList = game.Players:GetPlayers() |
05 | local chosenPlayer 1 = playerList [ math.random( 1 , #playerList) ] |
06 | table.remove(playerList, table.find(playerList, chosenPlayer 1 )) |
07 |
08 | local chosenPlayer 2 = playerList [ math.random( 1 , #playerList) ] |
09 | table.remove(playerList, table.find(playerList, chosenPlayer 2 )) |
10 | end |
11 | end ) |