randomplr = {} for _,v in pairs (game.Players:GetChildren()) do table.insert(randomplr, v.Name) chosenplayerone = game.Players:FindFirstChild(randomplr[1]) chosenplayertwo = game.Players:FindFirstChild(randomplr[2]) chosenplayerone.Status.Value = "Vigilante" chosenplayertwo.Status.Value = "Hero" end
I'm having a problem on output explaining a problem with "Argument 1 = nil", this is confusing me as I have been through many different methods and none work. It is running with two people, so that's not the problem. I have been blinded!
You don't need a for loop to get the players. Using :GetPlayers() already returns a table of players. You just need to make a function to select two random players - making sure the same one isn't selected twice.
NOTE : I did not input a fail-safe checking if there are at least two players.
local Players = Game.Players:GetPlayers() function GetTwoRandomPlayers() local Chosen = {} local first = Players[math.random(1, #Players)]; table.insert(Chosen, first) local second ; repeat second = Players[math.random(1, #Players)]; until second ~= Chosen[1]; table.insert(Chosen, second); return Chosen[1], Chosen[2] end local Player1, Player2 = GetTwoRandomPlayers(); Player1.Status.Value = "Vigilante"; Player2.Status.Value = "Hero";
randomplr = {} for _,v in pairs (game.Players:GetChildren()) do table.insert(randomplr, v.Name) end chosenplayerone = game.Players:FindFirstChild(randomplr[1]) chosenplayertwo = game.Players:FindFirstChild(randomplr[2]) chosenplayerone.Status.Value = "Vigilante" chosenplayertwo.Status.Value = "Hero"
You should select the players after going through the entire list, so your code referring to chosenplayerone
and chosenplayertwo
should happen after the for
loop end
s.
There's two things to keep in mind then. The first is that if you don't have at least two players in the game at this time, your script will error.
Second, the order won't be random! While the order may be in some strange order, every time the script is run (in a relatively close amount of time, i.e., within a few minutes) the order should be the same, so the same two people will be chosen over and over.
You have to either shuffle the list or pick random positions (picking random positions is much easier).
The modifications would look like this:
players = {} for _,v in pairs (game.Players:GetChildren()) do table.insert(players, v.Name) end chosenplayerone = game.Players:FindFirstChild( players[math.random(1,#players)] ); repeat chosenplayertwo = game.Players:FindFirstChild( players[math.random(1,#players)] ); until chosenplayertwo ~= chosenplayerone chosenplayerone.Status.Value = "Vigilante" chosenplayertwo.Status.Value = "Hero"
Here is another way to do it that doesn't involve the icky repeat until
loop:
players = {} for _,v in pairs (game.Players:GetChildren()) do table.insert(players, v.Name) end local randomIndex = math.random(1,#players) ; chosenplayerone = game.Players:FindFirstChild( players[randomIndex] ); table.remove(players,randomIndex); -- Remove the first person we picked chosenplayertwo = game.Players:FindFirstChild( players[math.random(1,#players)] ); chosenplayerone.Status.Value = "Vigilante" chosenplayertwo.Status.Value = "Hero"
We can make this still shorter by realizing that game.Players:GetChildren()
already stores a list of players! This eliminates the loop as well as the calls to FindFirstChild
:
players = game.Players:GetChildren(); local randomIndex = math.random(1,#players); chosenplayerone = players[randomIndex]; table.remove(players,randomIndex); -- Remove the first person we picked chosenplayertwo = players[math.random(1,#players)]; chosenplayerone.Status.Value = "Vigilante" chosenplayertwo.Status.Value = "Hero"