Add accessory to NPC from client

Here I have a LocalScript that is inside a GUI button. And when that button is pressed I want it to take the ID variable (that is a IntValue) and basically spawn the accessory on the NPC and when the player clicks on the button again, it removes that same accessory. I’ve tried using :AddAccessory() but it seems to be server sided only and when I tried with remoteEvents I would get errors for not being able to cast the object.

local Button = script.Parent
local ID = Button.Parent.ID

local NPC = game.Workspace.NPC

	if Button.Text == "Wear" then --put item on NPC
		Button.Text = "Take off"
		Button.Text = "Wear" --take off item on NPC

Well from a quick search on devforum it seems it is no longer possible locally (Post below)

So id recommend using a remote event or remote function depending on how you want to handle the accessory.
I tested it with InsertService with LoadAsset and it worked for me.

The code:


local InsertService = game:GetService("InsertService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RE = ReplicatedStorage.LoadAssetEvent

RE.OnServerEvent:Connect(function(player, id)
	local asset = InsertService:LoadAsset(id)
	local access = asset:GetChildren()[1]	--Gets accessory from model
	access.Parent = ReplicatedStorage
	access.Name = id



You can then grab the item from Replicated Storage by using the id again since we named it the same as the id or you can use a remote function and return the item path to your local script.

To remove it from the NPC simply find the accessory named after the id and destroy it. No need for remote events since you will handle NPC accessories locally.

I would recommend clearing the items you insert in Replicated Storage after it is equipped. You can simply add a task.wait at the end of the sever script and then destroy it. Otherwise you will end up with a lot of assets in your game.

On this line i get the error Unable to cast Instance to int64

1 Like

That means you didn’t give the function a valid ID. Chances are you gave it the path to the ID. I didn’t make the local script integrated with yours so you should use ID.Value rather than just id.
So firing the function would look like so


Lol my dumb mistake sorry it works now.
I’ve noticed that it doesn’t spawn on the NPC thought and is just somewhere random. Could this be fixed?

1 Like

Just parent the asset locally to the NPCs’ character.
As I mentioned above you can just use the ID to find it in Replicated Storage

1 Like

This is what I have in the LocalScript but it still doesn’t appear on the NPC.

local NPCItem = itemStorage:WaitForChild(ID.Value):Clone()
NPCItem.Parent = NPC

Well after looking into it further unless you are willing to manually weld the accessory it is needed to be done server sided. Post below

But you still need it locally so fastest solution I could think of would be to have a server NPC stored somewhere such as Replicated Storage, then equip the accessory with the server script. After that clone the NPC with a local script and parent it to workspace. Should work since the server script will properly equip the accessory before you clone it.
Hopefully this solution works!

Yep was able to make a new weld instance and connect the handle to the NPCs head. Thanks!

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