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)
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.