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

How would you find an Asset ID via a script?

Asked by 7 years ago
Edited 7 years ago

Me and my friend on Roblox were playing one of those old Roblox simulator games, and we came to the topic of removing hats if they were made after 2007. I suggested finding the asset ID of the last hat made in '07, and if the number is greater than that it would remove the hat.

The thing is, I don't know how you would format an asset ID to put in a script, if it is even possible (I've seen it done before, but...)

I don't know if you would put something like http://www.roblox.com/asset/?id= or something before it, or just put the number...

Thanks in advance.

game.Players.PlayerAdded:connect(function(playerAdded)
    player.CharacterAdded:connect(function(characterAdded)
        if characterAdded.Accessory. --i think i'm on the right track here but idk
            then character.Accessory.Handle:Destroy()
        end
    end)
end)

Edit: Here's what I think the script would look like. Yeah, all I've gotten to is the part where the player joins the game, and then the end part where the handle is destroyed if the assetID is above a certain number.

This probably will make all you experienced Lua users cringe, but this is what I have. Correct me if need be.

1 answer

Log in to vote
2
Answered by
nilVector 812 Moderation Voter
7 years ago
Edited 7 years ago

One method of doing this would be to loop through all the Hat objects that a player is wearing. Inside each hat, there should be a Part called "Handle" which contains a Mesh object inside of it. You can read the MeshId or the TextureId properties of this Mesh object to find the AssetId of the hat.

(Keep in mind, however, that ROBLOX is currently transitioning into turning Hat objects into Accessory objects.)

Now, first, let's look at the MeshId (you could use its TextureId if you want, but for my example, I'm using MeshId). MeshId is a property that holds a String something like this:

"rbxassetid://190770123"

However, we want just the numbers at the end, not the String. To do so, we would need to do utilize the match function for strings.

Here's how to do it:

-- New player enters the game
game.Players.PlayerAdded:connect(function(player) 
    -- The player's character loads/reloads
    player.CharacterAdded:connect(function(character)
        wait(1) -- Waits a second to allow accessories to load
        -- Loops through all the contents of the character
        -- Looks for any "Accessory" items
        for i, v in pairs(character:GetChildren()) do
            if v:IsA("Accessory") then -- We're only looking for Accessories
                local asset = v.Handle.Mesh.MeshId

                -- This is how to get only the numbers from the String
                local assetId = tonumber(asset:match("%d+"))
            end
        end
    end)
end)

Now comes the part where we need to see if the Accessory is from 2007 or not. If it isn't, we need to get rid of it. To do so, we need to use MarketplaceService.

Here's what the final script should look like after using MarketplaceService:

local marketplaceService = game:GetService("MarketplaceService")

-- New player enters the game
game.Players.PlayerAdded:connect(function(player) 
    -- The player's character loads/reloads
    player.CharacterAdded:connect(function(character)
        wait(1) -- Waits a second to allow accessories to load
        -- Loops through all the contents of the character
        -- Looks for any "Accessory" items
        for i, v in pairs(character:GetChildren()) do
            if v:IsA("Accessory") then -- We're only looking for Accessories
                local asset = v.Handle.Mesh.MeshId

                -- This is how to get only the numbers from the String
                local assetId = tonumber(asset:match("%d+"))

                -- Gets all the product information from the assetId
                local info = marketplaceService:GetProductInfo(assetId)

                -- Checks to see if the creation date is 2007 or not
                if string.sub(info.Created, 1, 4) ~= "2007" then
                    v:Destroy() -- Destroys accessory if not
                end
            end
        end
    end)
end)

Hope this helps! if it does, don't forget to click Accept Answer and +1 my Rep!

0
THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU SO MUUUCH Dried_Umeboshi 15 — 7y
0
You're welcome. nilVector 812 — 7y
0
It doesn't seem to remove the hats though. ._. I think I'm on the right track though. My friend was pretty impressed xD Dried_Umeboshi 15 — 7y
0
Really? I tested the script myself and it's working perfectly fine for me. nilVector 812 — 7y
View all comments (11 more)
0
Are you using the second block of code in my answer or the first one? The first one was just an example while the second one added on to it and was the final script. nilVector 812 — 7y
0
I'm using the second one. IDK why it's not working for me and working for you, as it's the exact same script. Studio hates me ;-; Dried_Umeboshi 15 — 7y
0
https://puu.sh/syXDs/711fd7a10d.gif here's a gif that paints a clearer picture of my problem Dried_Umeboshi 15 — 7y
0
In your script shown in the gif, you're missing the last two lines of code that I provided for you. There should be an "end) end)" at the end of your script just like in my answer. nilVector 812 — 7y
0
Those two are in there... odd. https://puu.sh/sz141/b0a9349e0e.png Dried_Umeboshi 15 — 7y
0
All I can say is: Make sure you copied my code exactly how it is in the answer. Also, make sure that the code is in a normal script (not a LocalScript or something) inside of Workspace. nilVector 812 — 7y
0
It's working perfectly fine for me. Look here (don't be confused by the invisible head, that's just a type of Head I'm wearing): https://gyazo.com/c5b35eaa9f1ad347024dc8738dfc1c51 nilVector 812 — 7y
0
i finally got it to work and i know you're gonna be mad when i found out how simple my problem was Dried_Umeboshi 15 — 7y
0
Why? What was the problem? nilVector 812 — 7y
0
I had set it to disabled in the beginning so it wouldn't mess up any other scripts running in my game... Well, I forgot to enable it again. But it works now, and my friend and I are using it in a 2007 Crossroads simulator. Dried_Umeboshi 15 — 7y
0
Oh. Such a simple mistake. Anyways, I'm glad it's finally working. nilVector 812 — 7y
Ad

Answer this question