Making an argument with multiple players using commas [Fixed]

Hey everyone,
I’ve recently been working on an admin system, and I was wondering if there is a good way to make an argument to reference multiple players but split them using a comma.

Thanks in advance if you help, I’ll be happy to answer any questions.

put them all in a string then use string.split()

local str = "one,two"
local p = string.split(str, ",")

p.foreach(function(i,v)
 print(v) -- prints one then two
end
1 Like

I’ll try it and let you know how it works, thank you for replying.

What is p.foreach for and is there any reason for not using use ipairs for arrays instead?

for k, v in ipairs(("one,two"):split(',')) do
    print(k, v) --> ouputs "one" then "two"
end
1 Like
local Players = game:GetService("Players")

local function getPlrsFromNameString(stringWithNames)
    local plrs = {}
    for i, s in ipairs(stringWithNames:split(",")) do
        local name = s:match("%S") -- non whitespace characters
        local plr = Players:FindFirstChild(name)
        if plr then
            plrs[#plrs+1] = plr
        else print("No player found with name "..name)
        end
    end
    return plrs
end
1 Like

Thank you for replying, you helped me with my last task of the day.

@RoBoPoJu I tested the code and I got one error in Players:FindFirstChild(Name). It’s a W000: Mismatch number and string error. Here’s my code:

elseif Key:find(",") then
        Key = Key:sub(1,#Key)
        local function getPlrsFromNameString(stringWithNames)
            for i, s in pairs(stringWithNames:split(",")) do
                local name = s:match("%S")
                local plr = Players:FindFirstChild(name) -- Error here
                if plr then
                    Players[#Players+1] = plr
                end
            end
        end
    else

Nevermind, it was Luau I just disabled it.