Need help with custom npc spawning script

Hi, so basically I made 2 scripts that would override a dummy’s avatar using GetHumanoidDescriptionFromUserId. I have a RemoteEvent Used for this that fires when you lose focus of the textbox and sends the player’s username through the textbox and converts to userId using GetUserIdFromNameAsync. The problem is that the script checking the textbox for the player’s name is a normal script so it just doesn’t work. But I can’t use a local script since it requires a normal script to use GetHumanoidDescription.

Is there anyway I can use the same variables for both of the scripts, or anything else?
Sorry if I made it confusing, I just don’t know how to explain it that well


LocalScript:

-- PLAYER 1
script.Parent.FocusLost:Connect(function()
	game.ReplicatedStorage.CopyCharDesc:FireServer()
end)

Script inside serverscriptservice:

game.ReplicatedStorage.CopyCharDesc.OnServerEvent:Connect(function(player)
	
	local char = game.Workspace[1]
	local textbox = game.StarterGui.ScreenGui.Frame.plr1
	
	local userid = game.Players:GetUserIdFromNameAsync(textbox.Text)
	local desc = game.Players:GetHumanoidDescriptionFromUserId(userid) 
	local animId = desc.IdleAnimation
	
	char.Humanoid:ApplyDescription(desc)
	
	local animation = game:GetService("InsertService"):LoadAsset(animId):GetChildren()[1]:GetChildren()[1]:GetChildren()[1]
	
	game:GetService("InsertService"):LoadAsset(animId).Parent = workspace
	
	local track = char.Humanoid:LoadAnimation(animation)
	track:Play()	
	
end)

First of all, you cannot use UI elements in a server script. Second, you shouldn’t be using StarterGui. Third, you can just use player.UserId to get the user ID. What you can do is when the focus is lost you can pass the text in the remote event, then access that argument you sent to get the user ID for another player. And also I don’t know what you are doing with the animation inserting. That’s not the way you should do it.

-- Local Script
script.Parent.FocusLost:Connect(function()
	game.ReplicatedStorage.CopyCharDesc:FireServer(script.Parent.Text)
end)
-- Server Script
game.ReplicatedStorage.CopyCharDesc.OnServerEvent:Connect(function(player, targetPlayerName)
	
	local char = game.Workspace[1]
	local userid = game.Players:GetUserIdFromNameAsync(targetPlayerName)
	local desc = game.Players:GetHumanoidDescriptionFromUserId(userid) 
	local animId = desc.IdleAnimation

    local animation = Instance.new("Animation")
    animation.AnimationId = animId
	
	char.Humanoid:ApplyDescription(desc)
	
	local track = char.Humanoid:LoadAnimation(animation)
	track:Play()	
	
end)
1 Like

thanks a lot sorry im just really new to scripting lol

1 Like