Hey there. So coming back from my break for a few hours to code a quick script on a testing server, just so I can see how I can make a random duo system.
So the way I was going to do this was to make a table titled ‘playerList’, and add players to it when they join. I add the players into the table by there names, and in the order of the number of players:
So after that I want to use math.random(1,#game.Players:GetPlayers()) to see which teammate the player will get. After that I want to get the player’s name through the table, but I can’t figure out how I can do that. What can I put in this string.find() as the arguments to see what the player’s spot in the table is, and what their name’s is? Thanks! (Let me know if you don’t understand.)
Full script:
local playerList = {}
function playerAdd(player)
if not string.find(player.Name, playerList) then
table.insert(playerList, #game.Players:GetPlayers(), player.Name)
end
if #game.Players == 10 then
local possibleTeamMates = math.random(1,#game.Players:GetPlayers())
if possibleTeamMates == string.find()
end
end
game.Players.PlayerAdded:Connect(playerAdd)
local playerList = {}
function playerAdd(player)
table.insert(playerList, #game.Players:GetPlayers(), player.Name)
while true do
wait(.25)
if #game.Players:GetPlayers() == 2 then
local possibleTeamMates = math.random(1,#game.Players:GetPlayers())
player.leaderstats.Teammate.Value = playerList[math.random(possibleTeamMates)].Name
end
end
wait(.25)
end
game.Players.PlayerAdded:Connect(playerAdd)
I believe your table.insert function is structured incorrectly. Because table.insert appends the value to the end of the current table, you should try:
playerList = game:GetService("Players"):GetPlayers();
function playerAdd(player)
table.insert(playerList, #game.Players:GetPlayers(), player.Name)
while true do
wait(.25)
if #game.Players:GetPlayers() == 2 then
local possibleTeamMates = math.random(1,#game.Players:GetPlayers())
player.leaderstats.Teammate.Value = playerList[math.random(possibleTeamMates)].Name
end
end
wait(.25)
end
game.Players.PlayerAdded:Connect(playerAdd)
Oh, I’m sorry. I meant you should remove the table.insert entirely.
Try this:
local Players = game:GetService("Players");
local playerList = Players:GetPlayers();
function playerAdd(player)
while true do
wait(0.25);
if #Players:GetPlayers() == 2 then
local possibleTeamMates = math.random(1,#Players:GetPlayers())
player.leaderstats.Teammate.Value = playerList[math.random(possibleTeamMates)].Name
end
end
wait(.25)
end
game.Players.PlayerAdded:Connect(playerAdd)
Disaster strikes when the playerList is empty or 1. Incredible!!!
local Players = game:GetService("Players");
local function onPlayerAdd(player)
while player.Parent do -- loop fixed
wait(0.25);
if #Players:GetPlayers() == 2 then
local possibleTeamMates = math.random(1,#Players:GetPlayers())
player.leaderstats.Teammate.Value = Players:GetPlayers()[math.random(possibleTeamMates)].Name
end
end
wait(.25)
end
Players.PlayerAdded:Connect(playerAdd)
I believe he wanted the playerList for comparison and other functions or he wouldn’t have it listed. But yes, if he didn’t, that would be a better solution.