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

AddAccessory does not work in-game, am I not waiting for something?

Sorry for one question so quickly after the other. In my last question, I was asking how to use AddAccessory properly. Now I've gotten my answer and it works perfectly in Studio, no bugs! However, when I launch a test server and then test it out, I click on the button that the hat comes from and absolutely nothing happens. This has happened before, and it was because I was not Finding First Child or I didn't wait for everything to be identified. Can someone help me quickly identify the thing I need to fix here so it will work in-game? The code is shown below:

function click()
	local accessory = game.Lighting.meshes.example:Clone()
	local player = game.Players.LocalPlayer
	local playercharacter = game.Players.LocalPlayer.Character
	local playername = player.Name
	local humanoid = game.Workspace:FindFirstChild(playername)
	
	--Hat Equips Here
	accessory.Parent = playercharacter
end

script.Parent.MouseButton1Click:connect(click)

2 answers

You were referencing the player's character twice, and naming the second reference "humanoid". Also, I don't see where you have used AddAccessory member function.

Here is the edited version

function click()
	local accessory = game.Lighting.meshes.example:Clone()
	local player = game.Players.LocalPlayer
	local playercharacter = player .Character or player.CharacterAdded:wait()
	local humanoid = playercharacter:WaitForChild("Humanoid", 2)

	--//After we have tried getting the humanoid, check if it is there
	if humanoid then
		--Hat Equips Here
		humanoid:AddAccessory(accessory)
	else
		print("No humanoid found")
	end
end

script.Parent.MouseButton1Click:connect(click)

I would recommend you also take a look at these:

Humanoid Class: http://wiki.roblox.com/index.php?title=API:Class/Humanoid

The AddAccessory member function of the humanoid: http://wiki.roblox.com/index.php?title=API:Class/Humanoid/AddAccessory

The Accessory, this is passed in the AddAccessory member function: http://wiki.roblox.com/index.php?title=API:Class/Accessory

5 comments

0

Oh, my bad! I did mention I was trying to use AddAccessory but in this version of my script I was not. Thank you for the correction, your method seems to work in studio. I'll verify it when it works in-game!

0

Alright, please tell me if it doesn't. I will be more than happy to help :)

0

It, unfortunately, does not work in-game either although both work perfectly in studio. I'm so confused.

0

Does it give you any error in the Developer Console?

0

Not at all. I press F9 and then click a button that gives the hat, and nothing shows up but the hat is not placed.

it doesn't work because AddAccessory() only works on the server, try to find another way to put the hat on

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