Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Humanoid:AddAccessory() does not work?

I am making a hat dispenser, when inputted an id, the dispenser will equip the hat to the character of the player. I am using a serverside script and a RemoteFunction to gain access to InsertService from the client. Everything works fine, there are no errors, but when it gets to Humanoid:AddAccessory(), it doesn't equip the hat.

Server script:


game.ReplicatedStorage.Insert.OnServerInvoke = function(player, id)
	local a = game:GetService("InsertService"):LoadAsset(id)
	print(a)
	a.Parent = game.ReplicatedStorage
	return a
end

Client Script:


local gui = script.Parent

gui.TextButton.MouseButton1Click:Connect(function()
	print(tonumber(gui.TextBox.Text))
	local acc = game.ReplicatedStorage.Insert:InvokeServer(tonumber(gui.TextBox.Text))
	print(acc)
	game.Players.LocalPlayer.Character.Humanoid:AddAccessory(acc:GetChildren()[1]) --This doesn't equip the hat.
end)

2 comments

0

To elaborate further, it does equip the accessory, but the accessory is underneath the baseplate, stuck. This must be an attachment problem.

0

I have fixed it by moving everything that has to do with the accessory to the client.

1 answer

It's not that Humanoid:AddAccessory() wasn't working, it was that the client has no memory of the server.

I fixed this by moving the line that calls Humanoid:AddAccessory() to the server, so now it looks like this:

Server Script:

game.ReplicatedStorage.Insert.OnServerInvoke = function(player, id)
	local a = game:GetService("InsertService"):LoadAsset(id)
	print(a)
	player.Character.Humanoid:AddAccessory(a:GetChildren()[1])
	return a
end

Client Script:


local gui = script.Parent

gui.TextButton.MouseButton1Click:Connect(function()
	print(tonumber(gui.TextBox.Text))
	local acc = game.ReplicatedStorage.Insert:InvokeServer(tonumber(gui.TextBox.Text))
end)

Hope this helps anyone who stumbles upon this post.

This discussion is preserved as it was. New answers, comments, and votes are closed.