Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to add Accessories to players in-game?

Asked by
rsh2 2
5 years ago

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.

0
Are you using a server script or local script? And you might have to own the clothing but I don't know. User#19524 175 — 5y
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. rsh2 2 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

:

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? rsh2 2 — 5y
Ad

Answer this question