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

How to add Accessories to players in-game?

I've made a script that allows players to enter a catalog ID and either purchase or wear the item. Currently, players can purchase items but not wear them. The following is the code I've been attempting to use to place items onto players. For simplicities sake, the only code for putting items on is for pants, but even that is off.

local wear = script.Parent.Parent.WearButton
local player = game.Players.LocalPlayer 


wear.MouseButton1Click:connect(function()
	local Id = script.Parent.Text
	local assetID = ("rbxassetid://"..Id) --asset link

	local avatarchange = player:GetChildren()-- Getting the children of player
	
	print ("Got to this part at least! and the id link is "..assetID) -- The furthest the code is run
	
	for i,v in pairs (avatarchange) do
		if (v.ClassName == "Pants") then
			print("Got to finding Pants")
			v.PantsTemplate = assetID 
		print("Pants have been succesfully worn")
 		
		end 
	end 
	
end)

There are no errors that pop up, but it simply does not run. The three things I'm sure about is:

  1. I most likely used the for function wrong
  2. I most likely need multiple if statements for whether the ID is accessory, shirt or pants.
  3. The accessories (from what I've seen), need weldfunctions. I've looked and am not quite sure how to implement this into the code.

Please, if you end up showing a proper script for this, explain it because this is in the realm that I have no expertise in.

2 comments

0

Are you using a server script or local script? And you might have to own the clothing but I don't know.

0

It's local script. I believe you don't need to own the clothing because it's literally grabbing ID's off the catalog itself and not within a group.

1 answer

Your avatarchange variable gets the contents of the player, not the player's character.

local wear = script.Parent.Parent.WearButton
local player = game.Players.LocalPlayer 


wear.MouseButton1Click:connect(function()
    local Id = script.Parent.Text
    local assetID = ("rbxassetid://"..Id) --asset link

    local characterContents = player.Character:GetChildren()

    print ("Got to this part at least! and the id link is "..assetID) -- The furthest the code is run

    for i,v in pairs (characterContents) do
        if (v.ClassName == "Pants") then
            print("Got to finding Pants")
            v.PantsTemplate = assetID 
        print("Pants have been succesfully worn")

        end 
    end 

end)

You can add accessories on the catalog (has to be owned by either you or ROBLOX) by using InsertService.

ex

local InsertService = game:GetService("InsertService")

local AssetID = 9999

function getAsset()
	local containerModel = InsertService:LoadAsset(AssetID)
	--when you load a asset, it's automatically stored into a model, so use a loop to get the asset
	local asset
	for i, v in pair(containerModel:GetChildren() do 
		asset = v
	end
	print("got asset: " .. asset.Name)
end

:

1 comment

0

You forgot a ')' on line 09 for the second excerpt. Anyhow, even after inputting the changes the code still wasn't executing. The squiggly blue line appears under 'getAsset' for not being a local variable. Once the script is successfully executed though, how does it get placed onto the character itself?

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