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:
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.
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 ¯_(ツ)_/¯
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.
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.
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
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.
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)!
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!