local allPlayers = {} for a,v in pairs(game.Players:GetPlayers(2)) do table.insert(allPlayers,v) end local plrAmount = game.Players.NumPlayers math.randomseed(tick()) local val1 = game.Players.NumPlayers, plrAmount local val2 repeat wait(5) val2 = game.Players.NumPlayers until val2 ~=val1 allPlayers[val1].Character.Torso.CFrame = CFrame.new (0,0,0) allplayers[val2].Character.Torso.CFrame = CFrame.new(665.995,7.1,-115.005)
First of all, you can't use HTML tags in Lua.
<pre class="brush: lua"> -- ... ... ... </pre> -- This isn't HTML, so this can't go in your script
Also, you can't put 2
in game.Players:GetPlayers(2)
. GetPlayers() is a function that gives you a table of the Players. It doesn't have any parameters.
I've added some annotations so you can read what is happening.
-- Pick two random players. -- This is accomplished by creating a table of all Players, then picking two random numbers to correspond with players in the table. local allPlayers = game.Players:GetPlayers() --Create a table of all the players -- #allPlayers is the amount of players in the game math.randomseed(tick()) -- Seed the randomizer so we can get a different sequence each time we generate one local val1 = math.random(1, #allPlayers) --Pick a random number between 1 and the amount of players (inclusive) local val2; --Predefine a variable for later repeat val2 = math.random(#allPlayers) --Pick a random number again until val2 ~=val1 --Keep picking a random number until it is different from the first random number -- Now teleport the players that correspond with the two random numbers allPlayers[val1].Character.Torso.CFrame = CFrame.new (0,0,0) allplayers[val2].Character.Torso.CFrame = CFrame.new(665.995,7.1,-115.005)
Also keep in mind this script will not work very well if there is only 1 player in the server. Be sure the script doesn't run unless there are two players in the server. I recommend checking the value of NumPlayers
if game.Players.NumPlayers > 1 then -- Code end