Does anyone know how to get an asset name from an asset id? I’ve tried doing this but it comes up with an error saying Unable to cast string to int64
.
while wait() do
local Text = script.Parent.TextBox.Text
local Asset = game:GetService("MarketplaceService"):GetProductInfo(Text)
script.Parent.TextLabel.Text = Asset.Name.." - "..Asset.Creator.Name
end
1 Like
You need to convert to a number because Text
is a string, when GetProductInfo
expects a number
while wait() do
local Text = tonumber(script.Parent.TextBox.Text)
if not Text then continue end
local Asset = game:GetService("MarketplaceService"):GetProductInfo(Text)
script.Parent.TextLabel.Text = Asset.Name.." - "..Asset.Creator.Name
end
Also, can’t you just listen for when the Text property changes instead of a loop?
local gui = script.Parent
gui.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
local Text = tonumber(gui.TextBox.Text)
if not Text then return end
local Asset = game:GetService("MarketplaceService"):GetProductInfo(Text)
gui.TextLabel.Text = Asset.Name.." - "..Asset.Creator.Name
end)
Thank you, this works. I also want it to return to no text when they delete the text it or if the asset doesn’t exist.
I think you’d have to pcall it
local gui = script.Parent
local MarketPlaceService = game:GetService("MarketplaceService")
gui.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
local Text = tonumber(gui.TextBox.Text)
if not Text then return end
local success, asset = pcall(MarketPlaceService.GetProductInfo,MarketPlaceService,Text)
gui.TextLabel.Text = success and asset.Name.." - "..asset.Creator.Name or ""
end)
If no asset exists for the given id, it errors, so if you contain it in a pcall, it’ll catch if it errored, and if it did, it just sets the text to be empty, else, it sets the text correctly
Accidentally forgot to not capitalize Asset
, should work now that all Asset
s are now asset
Also cause it catches errors, I don’t think if not Text then return end
is needed, so you can remove that I believe
3 Likes