Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do I Make An Npc Change Assets By Typing In The Username Of A Player?

Asked by 4 years ago

I am trying to make a game where you type in the username of a player and click play and it will do an animation As That Player.

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can use the :GetHumanoidDescriptionFromUserId method of the Players container as the foundation for this project.

The function returns a HumanoidDescription Object that contains the asset information of a linked Character retrieved by UserId cross-referencing. By using the :ApplyDescription() method of Humanoid, we can supply the HumanoidDescription Instance to actively draw the avatar of a Player onto a Model.

With a little filtering to derive the UserId of the Player Object pulled from our TextBox, and locating our Model's Humanoid, we can put everything together:

local Players = game:GetService("Players")

local AnimationModel = workspace:WaitForChild("AnimationModel")

local TextBox = script.Parent
local Button = TextBox:WaitForChild("PlayAnimation")

local CurrentDescription;

local function UpdatePlayerDescription(Return)
    if not (Return) then return end
    local Player = Players:FindFirstChild(TextBox.Text)
    if (Player) then
        local UserId = Player.UserId
        CurrentDescription = Players:GetHumanoidDescriptionFromUserId(UserId)
    end
end

local function SetModelDescription()
    local ModelHumanoid = AnimationModel:FindFirstChildOfClass("Humanoid")
    if (ModelHumanoid) then
        ModelHumanoid:ApplyDescription(CurrentDescription)
    end
end

TextBox.FocusLost:Connect(UpdatePlayerDescription)
Button.MouseButton1Click:Connect(SetModelDescription)

This is convenient as we're manipulating the Humanoid, which means Animation Loading and everything else will be pretty simple to integrate into this Script.


• Extra Resources:

Behaviour of RBXScriptSignal:

  1. .FocusLost
  2. .MouseButton1Click

If this helped, don't forget to accept this answer!

Ad

Answer this question