The easiest method that comes to mind would be through the use of HumanoidDescription. This way, Roblox does a lot of the work for you.
The HumanoidDescription of the player is modified so that the animations are of those that the Knight Animation uses.
There are two approaches for this:
I tend to prefer the first one, but either approach would work. As a sample for the code that you have provided, what you could do is:
local prompt = gui.TextBox
local AvatarEditorService = game:GetService('AvatarEditorService')
local AnimationTypes = {"IdleAnimation", "WalkAnimation", "JumpAnimation", "FallAnimation", "ClimbAnimation", "RunAnimation", "SwimAnimation"}
prompt.FocusLost:Connect(function(enterPressed)
if enterPressed and Player then
local animationId = tonumber(prompt.Text)
if animationId then
local Character: Model = Player.Character -- or Player.CharacterAdded:Wait() Removed as it is best to only accept a request when a Character is already present.
local Humanoid: Humanoid = Character:WaitForChild('Humanoid', 5)
-- Check if Humanoid is present and appearance is loaded.
if Humanoid and Player:HasAppearanceLoaded() then
local assetDetails = pcall(AvatarEditorService.GetItemDetails, AvatarEditorService, animationId, Enum.AvatarItemType.Asset)
if assetDetails and table.find(AnimationTypes, assetDetails.AssetType) then
-- Fetch description
local Description: HumanoidDescription = Humanoid:GetAppliedDescription()
-- Use AssetType as a key for properties in HumanoidDescription to check if animationId is the same. if this does not work, resort to if statements.
if Description[assetDetails.AssetType] ~= animationId then
-- Change property to match animation Id.
Description[assetDetails.AssetType] = animationId
-- Cache old description to :Destroy later.
local OldDescription = Humanoid:FindFirstChild('HumanoidDescription')
-- Reapply description (It can fail if Humanoid is not in the DataModel anymore, so wrap in pcall)
pcall(Humanoid.ApplyDescription, Humanoid, Description)
-- Dispose of old description
if OldDescription then
OldDescription:Destroy()
end
end
-- Dispose of description. (It is no longer useful)
Description:Destroy()
end
end
end
end
end)
In this simple sample, I have modified your code to fit the second approach. The first approach is quite similar to this in terms of description modification. In this, AvatarEditorService:GetItemDetails() is used to fetch information about the asset Id provided. The AssetType key can be used to check if the AssetType is an animation. Then, you can check the description to see if the animation ID is any different. You can modify the Id of the animation directly through the HumanoidDescription and reapply it to the Humanoid. The Id of the animation can be the product page as HumanoidDescription handles the rest of the asset management.
I have also removed the repeat that would wait for a character. If the user makes a request, it should not be accepted when the Character is not there yet.
I have left some comments in there in hopes that the approach is understandable. If there is any concern or questions, please feel free to ask! As you can tell I am kind of passionate about HumanoidDescription as an experience I develop for has made extensive use of those in the past year.