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

Add a accessory to a player?

if workspace:FindFirstChild("Pumpkin") then
	game.Players.PlayerAdded:Connect(function(player)
		local Na = player.Name
		workspace.Pumpkin.Parent = Na
	end) 
	end

There's 1 accesory named Pumpkin and I want every player to join to wear the pumpkin but I keep getting a error

3 answers

I usually just make the accessory's parent into StarterCharacterScripts

Humanoid documentation includes a function "AddAccessory" to do just that!

An example would be this:

if workspace:FindFirstChild("Pumpkin") then
    game.Players.PlayerAdded:Connect(function(player)
	-- Check if the character exists to prevent a crash
	if player.Character then
		local accessory = workspace.Pumpkin -- :Clone()
        	player.Character:WaitForChild("Humanoid"):AddAccessory(accessory)
	end
    end) 
end

or optionally you could do this:

if workspace:FindFirstChild("Pumpkin") then
    game.Players.PlayerAdded:Connect(function(player)
	-- Error handling
	local result,err = pcall(function()
		local accessory = workspace.Pumpkin -- :Clone()
        	player.Character:WaitForChild("Humanoid"):AddAccessory(accessory)
	end)
	if not result then
		warn(err) -- Display any errors without breaking the code
	end
    end) 
end

You can put the Pumpkin into ServerStorage:

game.Players.PlayerAdded:Connect(function(player)
player.Character.Humanoid:AddAccessory(game.ServerStorage.Pumpkin:Clone())
end)

Bad formatting on mobile

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