Pretty simple question, how do I use .Created in a script to remove accessories from players joining that were created before a certain year? I know I’m going to need to use ApplyDescription
to humanoids and all, but I’m just not sure how to put the script into fruition.
You could use the character’s HumanoidDescription to find the IDs for every accessory, check their creation, delete the ones you don’t want and apply it again.
The script would be something like this:
local MinYear = 2020
local Char -- This will be the player's character
local MarketplaceService = game:GetService("MarketplaceService")
-- Get all the accessories we need
local Description = Char.Humanoid:GetAppliedDescription()
local Accessories = Description:GetAccessories(true)
local ToRemove = {} -- Here we will keep accessories we don't want
-- Loop through accessories
for i, accessory in Accessories do
if tonumber(string.split(MarketplaceService:GetProductInfo(accessory.AssetId).Created,"-")[1]) < MinYear then
table.insert(ToRemove,accessory)
end
end
-- Remove all unwanted accessories
for _, accessory in ToRemove do
-- This substitues the ID of the accessory in the list with an empty string, effectively deleting it
local AccessoryType = string.sub(tostring(accessory.AccessoryType),20,-1)
Description[AccessoryType.."Accessory"] = string.gsub(Description[AccessoryType.."Accessory"],tostring(accessory.AssetId),"")
end
Char.Humanoid:ApplyDescription(Description)
Hope this helps!
I’m not exactly sure what’s going on in this script, but I don’t think this even is able to function. I don’t see anywhere that the script even uses (My fault @lamonLikeTinyGamer, I somehow completely skipped over:MarketplaceService
to use the asset ID to find the creation date of the asset.
if tonumber(string.split(MarketplaceService:GetProductInfo(accessory.AssetId).Created,"-")[1])
apologies!)
I’ve been trying to figure this problem out, and this is what I have gotten so far as a StarterPlayerScript:
local MS = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer -- Get the local player
-- Function to get character appearance information
local function getAppearanceInfo()
local userId = player.UserId -- Get the UserId of the local player
local success, info = pcall(function()
return game.Players:GetCharacterAppearanceInfoAsync(userId)
end)
if success then
for _, asset in ipairs(info['assets']) do
local AssetId = asset.id
print(AssetId) -- Printed Asset IDs will appear in the output window
end
else
warn("Failed to get character appearance info: " .. tostring(info))
end
end
-- Call the function when the player joins
getAppearanceInfo()
This gets the asset IDs of every asset currently on the player and pastes the IDs into output. I’m just trying to figure out how to get the creation date right now from the IDs gathered by this script.
I don’t really know anything about dictionaries or tables so maybe I’m just missing something about your script.
I don’t see anywhere that the script even uses
MarketplaceService
if tonumber(string.split(MarketplaceService:GetProductInfo(accessory.AssetId).Created,"-")[1]) < MinYear then
Their script works fine, too. At least, it started like so.
When I test it, it seems to error because it can’t find a ShoulderAccessory
property. The property in a HumanoidDescription that has them is actually, for whatever reason ShouldersAccessory
, so we need to add a check that adds the missing S.
Also, they forgot to call ApplyDescription
to actually apply these changes:
Character.Humanoid:ApplyDescription(Description)
With these in mind, here’s a cleaner version that removes old enough accessories every time the player spawns:
local MinYear = 2024 -- Change this
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
-- This fires every time a player joins
Player.CharacterAdded:Connect(function(Character)
-- And this fires every time the player's avatar starts loading.
-- Now that we have their character, begin removing
-- all accessories where their creation year < MinYear
local Description = Character.Humanoid:GetAppliedDescription() -- HumanoidDescription
local Accessories = Description:GetAccessories(true) -- Get the accessories from the description
for i, Accessory in Accessories do
-- Get the information of the accessory to find its creation date and look for the year
if tonumber(string.split(MarketplaceService:GetProductInfo(Accessory.AssetId).Created, "-")[1]) < MinYear then
-- Get the type of accessory so we can replace it in the description
local AccessoryType = string.sub(tostring(Accessory.AccessoryType), 20, -1) -- More string stuff...
AccessoryType = AccessoryType .. (AccessoryType == "Shoulder" and "s" or "") -- Append "s" if it goes on the shoulders
Description[AccessoryType .. "Accessory"] = string.gsub(Description[AccessoryType .. "Accessory"], tostring(Accessory.AssetId), "") -- Remove
end
end
Character.Humanoid:ApplyDescription(Description) -- Apply the changes!
end)
end)
This can take a while because GetProductInfo
is a yielding function (it freezes the thread until done), you’ll have to deal with that yourself. I tested this one and it works perfectly on my avatar.