Random Avatar Gen

Hello! I want to know how to make every player who joins the game have a different avatar, and have specific players have an ‘Exclusive Suit’ how might I do this?
Here are the avatar’s I’ve created:


The exclusive avatar is the specific player only suit.

2 Likes

There are probably other ways to do this, but a possible solution could be creating a folder with HumanoidDescriptions for each custom character. Then, when a player joins, checking if they are eligible for the exclusive avatar, and if not, getting a random HumanoidDescription and using humanoid:ApplyDescription() to apply it to the character.

1 Like

I have tried this, but I haven’t been able to make it work, it also included about 100 lines of code for a simple script. But then again I suck at scripting so ¯_(ツ)_/¯

1 Like

I can provide a simple way about selecting a random avatar from a selection provided they’re all within a Folder:

local Avatars = Folder:GetChildren()
local RandomAvatar = Avatars[math.random(1, #Avatars)]
1 Like

Would this folder just remain in Workspace or a place like RepStorage or ServerStorage?

1 Like

I personally would put them inside of ServerStorage since the client has no reason to see the selection of Avatars, only the Server does since it’ll be the one applying the Avatars.

1 Like

The script should only take a few lines of code, as @Youtub5005 said you can do this to get the random avatar:

local descs = [[REPLACE WITH FOLDER]]

local avatars = descs:GetChildren()
local randomAvatar = avatars[math.random(1, #avatars)]

and then apply the avatar through this:

character:WaitForChild("Humanoid"):ApplyDescription(randomAvatar)

Obviously this isn’t the full code, but these are the main things you need other than getting the character. You can put your script in serverScriptService and put your folder in serverStorage.

2 Likes

Here is the current script, but it doesn’t load the character in.

-- Services
local players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

-- Variables
local remoteEvent = replicatedStorage.RemoteEvents.Outfit
local outfitsArray = serverStorage.CharactersDefault:GetChildren()

-- Methods and Statements
--players.CharacterAutoLoads = false

players.PlayerAdded:Connect(function(player)
	LoadCustomCharacter(player)
end)

remoteEvent.OnServerEvent:Connect(function(player)
	LoadCustomCharacter(player)
end)

-- Functions
function LoadCustomCharacter(player)
	player.Character.Humanoid:ApplyDescription(outfitsArray[math.random(1, #outfitsArray)].Humanoid.HumanoidDescription)
end
1 Like

players.PlayerAdded doesn’t necessarily guarantee that the Player’s Character will exist yet. Try hooking up a player.CharacterAdded Connection every time a Player joins

1 Like

As @Youtub5005 said, you need to make sure that the character is added to load the description. Here are a few suggestions as there are some other errors in your script.

First make sure the character is loaded:

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		LoadCustomCharacter(char)
	end)
end)

You can now send the character to your LoadCustomCharacter function and can revise it to also prevent the " Humanoid:ApplyDescription() DataModel was not available" error. Read this article to understand the fix I added!

local function LoadCustomCharacter(char)
	local randomAvatar = outfits[math.random(1, #outfits)]
	local Humanoid = char:WaitForChild("Humanoid") -- get the humanoid
	
	if not Humanoid:IsDescendantOf(game) then -- will prevent the annoying error :D
		Humanoid.AncestryChanged:Wait()
	end
	
	Humanoid:ApplyDescription(randomAvatar)
end

With these changes your code should now work (at least when I tested it did)!

3 Likes

are you using a remoteevent for this? It’s difficult to understand what is being used in your studio file, because the results of it could be completely different. Would it be possible to send your version of the script?

At the time I was not using a remote event in my script. However after adding one in it worked with the code I suggested, the only difference being you would have to get the character when the event is fired.

I strongly suggest you make sure your script is doing this and correctly applies my earlier comments before using this script below as you will learn NOTHING from just copying it.

Server Script
local players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = replicatedStorage.RemoteEvents.Outfit
local outfits = serverStorage.CharactersDefault:GetChildren()

local function LoadCustomCharacter(char)
	local randomAvatar = outfits[math.random(1, #outfits)]
	local Humanoid = char:WaitForChild("Humanoid") -- get the humanoid

	if not Humanoid:IsDescendantOf(game) then -- will prevent the annoying error :D
		Humanoid.AncestryChanged:Wait()
	end

	Humanoid:ApplyDescription(randomAvatar)
end

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		LoadCustomCharacter(char)
	end)
end)
remoteEvent.OnServerEvent:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait() -- get the character
	
	LoadCustomCharacter(char)
end)

If this doesn’t work it probably means there may be something wrong with the code the fires the event!