Character Randomizer

Hey there! I’m currently trying to make a system where when a player joins they are assigned a preset custom character. This character will be different for every player. I have 10 different characters made.

The issue is, when one player joins, multiple StarterCharacters move into the StarterPlayer. Also, everybody spawns as the same person.

Screenshot_215

Any help is appreciated!

You could use a HumanoidDescription for this. Make one for each character and use Humanoid:ApplyDescription(HumanoidDescription) to apply it.

1 Like

Idk if this could help but
I would do like this

Create the first table of UsableCharacters{Adam,Emily,Isaac} and so on
UsedCharacters{} Empty

UaableCharNumber = 10

Then on player join do
Table.Move(UsableChar) with a random.math(1,UsableCharNumber) and move the random selected to UsedCharacters and a Table.Remove from the UsableCharacters
UsableCharNumber = UsableCharNumber - 1

So basically

You have called a table with inside the 10 characters

On player join randomically one of 10 will be moved to another table and removed from the first one, now you will have 9 in usable table and 1 on used table

Once you did this you can run a custom humanoid or custom character to upload it

I’m from mobile and is 3:30AM from here, if you need a better explaination i’ll share an example of a code with the same working method

Hope it helps

1 Like

Try this, I just finished it:

local cs = workspace.CharacterSelect

game.Players.ChildAdded:Connect(function()
    local charTable = {
        {Name = cs.Adam, num = 0, taken = false},
        {Name = cs.Marcos, num = 0, taken = false},
        {Name = cs.Bryan, num = 0, taken = false},
        {Name = cs.Chad, num = 0, taken = false},
        {Name = cs.Chelsea, num = 0, taken = false},
        {Name = cs.Emily, num = 0, taken = false},
        {Name = cs.Issac, num = 0, taken = false},
        {Name = cs.Todd, num = 0, taken = false},
        {Name = cs.Mike, num = 0, taken = false}
    }

    -- Function to scramble the characters in the table
    local function scrambleTable()
        for _, character in pairs(charTable) do
            character.num = math.random(1, 100)
        end

        table.sort(charTable, function(a, b)
            return a.num < b.num
        end)
    end

    -- To pick a character for the player to become
    local function pickCharacter()
        for _, pick in pairs(charTable) do
            if taken == false then
                local character = pick.Name
                local randchar = character:Clone()
                randchar.Name = "StarterCharacter"
                randchar.Parent = game.StarterPlayer
                taken = true
                return
            end
        end
    end

    scrambleTable()
    pickCharacter()
    
end)
1 Like

It doesn’t seem to be working nor does the StarterCharacter appear in StarterPlayer
image

1 Like

Try placing print()’s in different places of the script to see if some parts of it don’t work.

Also, look into the pickCharacter() function in particular and see if I’m assigning the player incorrectly - in this part:

1 Like

I just tried it and it seems like nothing is wrong, I’m getting no errors in the output.
The only thing I can find “wrong” is this:
image

Ah I’m sorry, I messed up on that part. The problem is that I was supposed to put “pick.taken”. Here’s the full script with just that changed:

local cs = workspace.CharacterSelect

game.Players.ChildAdded:Connect(function()
    local charTable = {
        {Name = cs.Adam, num = 0, taken = false},
        {Name = cs.Marcos, num = 0, taken = false},
        {Name = cs.Bryan, num = 0, taken = false},
        {Name = cs.Chad, num = 0, taken = false},
        {Name = cs.Chelsea, num = 0, taken = false},
        {Name = cs.Emily, num = 0, taken = false},
        {Name = cs.Issac, num = 0, taken = false},
        {Name = cs.Todd, num = 0, taken = false},
        {Name = cs.Mike, num = 0, taken = false}
    }

    -- Function to scramble the characters in the table
    local function scrambleTable()
        for _, character in pairs(charTable) do
            character.num = math.random(1, 100)
        end

        table.sort(charTable, function(a, b)
            return a.num < b.num
        end)
    end

    -- To pick a character for the player to become
    local function pickCharacter()
        for _, pick in pairs(charTable) do
            if pick.taken == false then
                local character = pick.Name
                local randchar = character:Clone()
                randchar.Name = "StarterCharacter"
                randchar.Parent = game.StarterPlayer
                taken = true
                return
            end
        end
    end

    scrambleTable()
    pickCharacter()
    
end)
2 Likes

That fixed the issue of the characters spawning, however the same issue as before still remains where all the characters are the same.

1 Like

Oh my goodness!! I’m sorry. I completely forgot to fix the other part in the script where it says “taken = true”. I’ll change it to the correct one, so that it’ll mark when a character is taken already.

I looked over it a few times and I really hope it works this time:

local cs = workspace.CharacterSelect

game.Players.ChildAdded:Connect(function()
    local charTable = {
        {Name = cs.Adam, num = 0, taken = false},
        {Name = cs.Marcos, num = 0, taken = false},
        {Name = cs.Bryan, num = 0, taken = false},
        {Name = cs.Chad, num = 0, taken = false},
        {Name = cs.Chelsea, num = 0, taken = false},
        {Name = cs.Emily, num = 0, taken = false},
        {Name = cs.Issac, num = 0, taken = false},
        {Name = cs.Todd, num = 0, taken = false},
        {Name = cs.Mike, num = 0, taken = false}
    }

    -- Function to scramble the characters in the table
    local function scrambleTable()
        for _, character in pairs(charTable) do
            character.num = math.random(1, 100)
        end

        table.sort(charTable, function(a, b)
            return a.num < b.num
        end)
    end

    -- To pick a character for the player to become
    local function pickCharacter()
        for _, pick in pairs(charTable) do
            if pick.taken == false then
                local character = pick.Name
                local randchar = character:Clone()
                randchar.Name = "StarterCharacter"
                randchar.Parent = game.StarterPlayer
                pick.taken = true
                return
            end
        end
    end

    scrambleTable()
    pickCharacter()
    
end)
2 Likes

Hmm. I don’t think your script is wrong I just don’t think there can be multiple StarterCharacters in the StarterPlayer.

We all still spawn as the same character. Let me know if you find any solutions, I’ll keep testing stuff

I believe this would not work because you are assigning a fresh charTable every time a new player joins.

@Salinas23 Ahh you’re right!

I think the problem might be that you’re using “game.StarterPlayer” instead of the specific player’s StarterPlayer. I switched the function to “PlayerAdded”, and specifically referred to the player’s StarterPlayer. I’m not sure if it’s possible to do this through a server script, however we’ll see how it goes.

Also, I accidently used “return” instead of “break” at one point.

Try this, and tell me what works/doesn’t and what it prints (added prints to check on table):

local cs = workspace.CharacterSelect

local charTable = {
    {Name = cs.Adam, num = 0, taken = false},
    {Name = cs.Marcos, num = 0, taken = false},
    {Name = cs.Bryan, num = 0, taken = false},
    {Name = cs.Chad, num = 0, taken = false},
    {Name = cs.Chelsea, num = 0, taken = false},
    {Name = cs.Emily, num = 0, taken = false},
    {Name = cs.Issac, num = 0, taken = false},
    {Name = cs.Todd, num = 0, taken = false},
    {Name = cs.Mike, num = 0, taken = false}
    }

Players.PlayerAdded:Connect(function(player)

-- Check what shows up in table at start of ChildAdded
    for _, char in pairs(charTable) do
        print("CharName: " .. char.Name)
        print("Num: " .. char.num)
        print("Taken: " .. char.taken)
    end

    -- Function to scramble the characters in the table
    local function scrambleTable()
        for _, character in pairs(charTable) do
            character.num = math.random(1, 100)
        end

        table.sort(charTable, function(a, b)
            return a.num < b.num
        end)
    end

    -- To pick a character for the player to become
    local function pickCharacter()
        for _, pick in pairs(charTable) do
            if pick.taken == false then
                local character = pick.Name
                local randchar = character:Clone()
                randchar.Name = "StarterCharacter"
                randchar.Parent = player.StarterPlayer
                pick.taken = true
                break
            end
        end
    end

    scrambleTable()
    pickCharacter()

-- Check what shows up in table after everything else (see if everything worked)
    for _, char in pairs(charTable) do
        print("CharName: " .. char.Name)
        print("Num: " .. char.num)
        print("Taken: " .. char.taken)
    end
    
end)
1 Like

Hmm, this is the only error I got in the output

Also, I changed Players.PlayerAdded:Connect(function(player)
to
game.Players.PlayerAdded:Connect(function(player)

Before I changed it there was an error
image

Oh I see, I accidently tried to concatenate the actual character object and act like it’s a string.

And yeah, whoops. Good thing you fixed it.

Ah I see, I’ll just remove the print checks. Here:

local cs = workspace.CharacterSelect

local charTable = {
    {Name = cs.Adam, num = 0, taken = false},
    {Name = cs.Marcos, num = 0, taken = false},
    {Name = cs.Bryan, num = 0, taken = false},
    {Name = cs.Chad, num = 0, taken = false},
    {Name = cs.Chelsea, num = 0, taken = false},
    {Name = cs.Emily, num = 0, taken = false},
    {Name = cs.Issac, num = 0, taken = false},
    {Name = cs.Todd, num = 0, taken = false},
    {Name = cs.Mike, num = 0, taken = false}
    }

Players.PlayerAdded:Connect(function(player)

    -- Function to scramble the characters in the table
    local function scrambleTable()
        for _, character in pairs(charTable) do
            character.num = math.random(1, 100)
        end

        table.sort(charTable, function(a, b)
            return a.num < b.num
        end)
    end

    -- To pick a character for the player to become
    local function pickCharacter()
        for _, pick in pairs(charTable) do
            if pick.taken == false then
                local character = pick.Name
                local randchar = character:Clone()
                randchar.Name = "StarterCharacter"
                randchar.Parent = player.StarterPlayer
                pick.taken = true
                break
            end
        end
    end

    scrambleTable()
    pickCharacter()
    
end)
1 Like

Don’t do that, if you clone a NPC and name it StarterCharacter, the others can have the same Avatar or Character, put this

function PickCharacter(plr)
   for _, pick in pairs(charTable) do
       if pick.taken == false then
          local character = pick.Name
          local randchar = character:Clone()
          plr.Character = randchar
          pick.taken = true
          break
      end
   end
    
end
1 Like

Your solution and @MJTFreeTime’s solution don’t seem to be working. I get an error when I replace the PickCharacter function with your solution.
I really appreciate the support I’ve been getting on this thread and hopefully we can get this solved soon! :smiley:

1 Like

Oh strange, what’s the error?

And, does an error only show for @Nube762’s solution, or mine as well?

1 Like