I'm doing a game and I need the players to be one by one in a place for 50 seconds, it's like fashion famous or dance off, it's like a parade, I try random player but the players repeat themselves. My script:~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~local Player = game.Players:GetPlayers(1) local Player2 = game.Players:GetPlayers(2) local Player3 = game.Players:GetPlayers(3) local Player4 = game.Players:GetPlayers(4) local Player5 = game.Players:GetPlayers(5) local Player6 = game.Players:GetPlayers(6) local Player7 = game.Players:GetPlayers(7) local Player8 = game.Players:GetPlayers(8) local Player9 = game.Players:GetPlayers(9) local Player10 = game.Players:GetPlayers(10) local pos1 = Vector3.new(-62, 2.522, 31) local pos2 = Vector3.new(63.766, 8.1, 141.166) wait(123) Player.Character:MoveTo(pos1) wait(50) Player.Character:MoveTo(pos2) Player2.Character:MoveTo(pos1) wait(50) Player2.Character:MoveTo(pos2) Player3.Character:MoveTo(pos1) wait(50) Player3.Character:MoveTo(pos2) Player4.Character:MoveTo(pos1) wait(50) Player4.Character:MoveTo(pos2) Player5.Character:MoveTo(pos1) wait(50) Player5.Character:MoveTo(pos2) Player6.Character:MoveTo(pos1) wait(50) Player6.Character:MoveTo(pos2) Player7.Character:MoveTo(pos1) wait(50) Player7.Character:MoveTo(pos2) Player8.Character:MoveTo(pos1) wait(50) Player8.Character:MoveTo(pos2) Player9.Character:MoveTo(pos1) wait(50) Player9.Character:MoveTo(pos2) Player10.Character:MoveTo(pos1)~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~ This script no found
First of all, game.Players:GetPlayers()
does not accept any arguments, so giving it 1
will not give you the 1st player. It returns a table, so if you are certain you have at least one player (which I think is almost always the case), you can do game.Players:GetPlayers()[1]
- or, better, assign it to a variable first:
local players = game.Players:GetPlayers() print(players[1]) -- or do whatever you want with it
Of course, in your case you don't want to do something with all players, not just the 1st one that GetPlayers happens to return.
So, see how your script has a bunch of repeated code? That's a clue you need to use a for
loop (for syntax, see https://www.lua.org/pil/4.3.4.html). Also, you need to understand tables/lists, so here's a quick demo (just in case you didn't know, everything after a --
is a comment):
local list = {} -- make a new list list[1] = "hello" -- "hello" becomes the first item in the list. One could also say that we're assigning the value "hello" to the numeric key "1". list[2] = "there" print(#list) -- "#" is the number of items in a list. It only works if you use integer keys (like 1, 2, etc), and don't have holes (ex if you do 1, 3, 5, it may not work correctly). print(list[1], list[2]) -- will print hello there for i = 1, #list do -- repeat the loop for each item in the list print(i, list[i]) -- print the index 'i' followed by the value. First time it will print "1 hello", the 2nd time "2 there" end list[2] = nil -- removes the item from the list. You can also use table.remove and table.insert to remove and add/insert items. print(#list) -- will print 1
Now we could do this simple script:
local pos1 = Vector3.new(-62, 2.522, 31) local pos2 = Vector3.new(63.766, 8.1, 141.166) wait(123) local players = game.Players:GetPlayers() for i = 1, #players do local player = players[i] player.Character:MoveTo(pos1) wait(50) player.Character:MoveTo(pos2) end
It's your script, but fixed to use for
loops and GetPlayers
correctly. However, it has these problems:
player.Character
without checking to see if they have a Character (though actually I'm not 100% sure that there's a time they don't - but it's better to be safe) - even if they have a Character, their character might be dead (and about to respawn), so we should at least check that.I don't tackle the 2nd point, but here's an improved version:
local pos1 = Vector3.new(-62, 2.522, 31) local pos2 = Vector3.new(63.766, 8.1, 141.166) local selectedPlayers = {} -- dictionary of players selected so far local function playerIsSelectable(player) -- Only allow a player to be selected if they are alive and haven't been selected yet local humanoid = player.Character and player.Character:FindFirstChild("Humanoid") return not selectedPlayers[player] and humanoid and humanoid.Health > 0 end local function selectRandomPlayer() local players = game.Players:GetPlayers() local validPlayers = {} for i = 1, #players do local player = players[i] if playerIsSelectable(player) then validPlayers[#validPlayers + 1] = player end end local selected = validPlayers[math.random(1, #validPlayers)] selectedPlayers[selected] = true -- record that we're selecting this player return selected end wait(123) while true do local player = selectRandomPlayer() if not player then break end -- either selected all the players or no other players are ready/selectable at the moment player.Character:MoveTo(pos1) wait(50) player.Character:MoveTo(pos2) end
how to select player 1, player 2 one by one?
(Rushed answer sorry, accept answer if this helped)
Iteration, iterate through the player list.
game:GetService("Players")
gets the players list
players:GetPlayers()
returns array of all players.
local players = game:GetService("Players") --// get players service local position = Vector3.new(...) --// assumed reference to a position for i, player in next, players:GetPlayers() do --//iterate through players local character = player.Character --// get character if character then --// make sure there is a character character:MoveTo(position) end end
What you want are tables.
I've provided some pseudocode so that you can see roughly what your finished script needs to do, without literally just handing you the solution.
playersList = game:GetService("Players"):GetPlayers() position1 = Vector3.new(x1, y1, z1) position2 = Vector3.new(x2, y2, z2) for each index, player in playersList: -- index is the index in playersList of the corresponding player if index is even: pos = position1 else pos = position2 end if move player to pos end for
To test if something is even, you can use %
, which gets the remainder of a division.
5 ÷ 2 is 2, with 1 left over. This 1 is known as the remainder.
8 ÷ 2 is 4, with a remainder of 0.
You can probably see that any number is even if and only if the remainder is 0 when it's divded by 2.
x = 8 if x % 2 == 0 then print("x is even") else print("x is odd") end
You can probably play with that example if it's not clear ^
I do not want to move all the players, only one by one, first one then I wait 50 seconds, then that one goes away and the next one appears for 50 seconds and so