I want to make it so that the player enters a user ID attached to an in-game NPC and the NPC changes it’s appearance to look like the ID’s user.
The issue starts when I enter the user ID, the game processes it trough a module script with the intended function to change the HumanoidDescription of the NPC, it prints everything out (I made a print after almost every line in the function) and the NPC stays untouched.
I’ve scraped the developer hub for answers but couldn’t find any.
Here’s the module function:
function module.ChangeCharacter(ID, CharacterToChange)
if tonumber(ID) then
print(1)
if ID ~= 0 then
print(2)
local newHumanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(ID)
print(ID)
CharacterToChange.Humanoid:ApplyDescription(newHumanoidDescription)
print(3)
else -- get char back to normal
print(4)
local humanoid = AnimsFolder[game.ReplicatedStorage.currentAnim.Value].Characters[CharacterToChange.Name].Humanoid
local newHumanoidDescription = Instance.new("HumanoidDescription")
humanoid:ApplyDescription(newHumanoidDescription)
print(5)
end
end
end
and here’s the ID and character being passed trough:
local name
for q, a in pairs(info[1]) do
if a.Name == v.name.Text then
name = a
end
end
changeChar(v.ID.Text, name)
print(name)
Sorry for the code being unclear! If you need to, I’ll explain everything in detail. Thanks!
Your second piece of code seems very unclear and there seems to be a few mistakes in the first one.
In your first script, you are doing a tonumber but are not saving the result. In Lua, values are passed as values and not as reference (only tables are). So you must do this:
ID = tonumber(ID)
Also, I don’t understand your else section. You are getting a humanoid (idk from where you are pulling this) and you create a brand new HumanoidDescription. The weird part here is that you pass in the exact Character you want to change the look for, but then you go pull this humanoid from seemingly a weird place… Just do this:
local humanoid = CharacterToChange:FindFirstChildOfClass("Humanoid")
humanoid:ApplyDescription(Instance.new("HumanoidDescription"))
In your second script, it seems you are passing a name? Make sure you are passing a Character Model.
The tonumber is to just check if the ID is a number, because let’s say the player enters a user’s username instead of their ID, I’m pretty sure it’ll break. And I think that that’s the issue here. The ID from the texbox is a string value, and I didn’t convert it. Here are the other answers if for some reason you need them.
The unclear peace of code cleared up:
Firstly a bit of context. the for q, a loop is inside a function. Now there is a for i, v loop in which the function is in, and the i, v (v are all the text boxes that the player types the ID in) loop is attaching the said function to the TextBox “FocusLost” event.
local char
for _, character in pairs(info[1]) do -- info[1] being the character list
if character.Name == v.name.Text then -- "name" being the changable character's name in the GUI, basically gets the character we want to change.
char = character
end
end
changeChar(v.ID.Text, char)
print(name)
About the else section. If I was to explain where I got the humanoid I’d need to write ten lines of text just explaining it, so if you don’t necessarily need it, I wouldn’t answer it. And about this:
The weird part here is that you pass in the exact Character you want to change the look for
You’re absolutely right! I should’ve just used CharacterToChange.Humanoid. It gets the exact same humanoid. It was just late at night and I kinda just went too deep into the zone to notice.
You might be giving the function the ID as a string instead of a number
The reason why I think this is because when you’re calling the function, you’re sending in text. changeChar(v.ID.Text, name)
You tell your function to do nothing if it is given text.
How about trying something like this instead:
function module.ChangeCharacter(ID, CharacterToChange)
assert(ID and typeof(ID) == "number", "argument 1 must be a number")
assert(CharacterToChange and CharacterToChange:FindFirstChild("Humanoid"), "argument 2 must be a character")
print(1)
if ID ~= 0 then
print(2)
local newHumanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(ID)
print(ID)
CharacterToChange.Humanoid:ApplyDescription(newHumanoidDescription)
print(3)
else -- get char back to normal
print(4)
local humanoid = AnimsFolder[game.ReplicatedStorage.currentAnim.Value].Characters[CharacterToChange.Name].Humanoid
local newHumanoidDescription = Instance.new("HumanoidDescription")
humanoid:ApplyDescription(newHumanoidDescription)
print(5)
end
end
and then converting the text to a number before running it: changeChar(v.ID.Text, name)
If this doesn’t work, try printing out the values print(ID, CharacterToChange) at the top of the function so we can see what they actually are.