CreateHumanoidModelFromUserId always spawns the same avatar

So, I wanted to make a thing where you type an username onto a TextBox, and then you hit Enter and it generates the player model of the avatar with that username.

The thing is, when I hit enter, regardless of the username, still spawns the same avatar.


The code:

local charname = script.Parent.Charname
local enter = script.Parent.Enter
local char = charname.SurfaceGui.TextBox.ContentText
	enter.ClickDetector.MouseClick:Connect(function()
	local id = game.Players:GetUserIdFromNameAsync(char)
	local model = game.Players:CreateHumanoidModelFromUserId(id)
	model.Parent = workspace
	model.Name = char
end)

Now, yesterday I thought that the id variable wouldn’t constantly update, and adding a while wait(0) do loop would help, but that just made the character not spawn at all. Then I realized that the script would update when the “Enter” part was clicked, so the while loop wouldn’t do anything, other than break the script. But now I have the same script from earlier, and therefore the same problem.

The character model generator thing is really just a test to improve my scripting, along with pretty much everything on the screenshot. But i’ve yet to find a solution to this, so it would be nice for you guys to help me out.

1 Like

move the line
local char = charname.SurfaceGui.TextBox.ContentText
under
enter.ClickDetector.MouseClick:Connect(function()

You are getting the textbox’s content when the script runs, not when the button is pressed

1 Like

Didn’t do anything


New code:

local charname = script.Parent.Charname
local enter = script.Parent.Enter
	enter.ClickDetector.MouseClick:Connect(function()
	local char = charname.SurfaceGui.TextBox.ContentText
	local id = game.Players:GetUserIdFromNameAsync(char)
	local model = game.Players:CreateHumanoidModelFromUserId(id)
	model.Parent = workspace
	model.Name = char
end)

Is this a localscript? The text doesn’t replicate to the server when you edit it.

It’s just a regular script, not a localScript

Yeah you have to use it in a localscript, or fire a remoteevent to the server and pass the edited text thru it from the client, when you edit it on the client, the server still sees the original value, that’s why it doesnt change.

Ok so when you put a new text, it remains client-sided and doesn’t replicate to the server. I’ll try your solution and if it works I’ll mark what you said as the solution.

2 Likes

I put the code in a localScript and it doesn’t spawn anything

Show your code and the explorer.

I haven’t done any modifications to the code, so it still looks identical to the previous code.
This is the explorer: (ignore the other models)
image

Localscripts don’t run in workspace, place it into StarterPlayerScripts or StarterCharacterScripts, and edit the references in your script.

image
image
It still doesn’t work


(also i just realized that I put the 2 variables as the same thing)

local enter = game.Workspace.Model.Charname is supposed to be local enter = game.Workspace.Model.Enter

IT WORKED YAY

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.