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

Why is it say "Unable to cast string to int" , someone can help me?

Asked by 6 years ago
local SoundId = game.Workspace.MusicPlayer.MusicScript.Sound.SoundId
local Asset = game:GetService("MarketplaceService"):GetProductInfo(SoundId)
while true do
    script.Parent.Text = "Music name ="..Asset.Name.."/ ID ="..SoundId..""
    wait(5)
end

Output---------------------------

13:50:18.076 - Unable to cast string to int

13:50:18.077 - Stack Begin

13:50:18.078 - Script 'Players.Sphynks.PlayerGui.MUsci.TextLabel.LocalScript', Line 2

13:50:18.079 - Stack End

2 answers

Log in to vote
0
Answered by 6 years ago

Hey Sphynks,

I was looking into your issue and the issue that you're having is but, a tiny one. The only thing that's happening is when you get the SoundId, it comes in a String, first off, you need to get the numbers from this String because, a SoundId comes in the following order:

rbxassetId://[ID]

Well, you need to get the ID from that string and it's very simple to do that with a bit of string patterns and methods. You are going to use the :match() method of strings to accomplish this. Below is an example of how you'd do that. I'm going to use the id "rbxassetid://787837201" for this example.

Example A

local id = "rbxassetid://787837201";
local real_id = id:match("%d+") -- Returns the numbers in the string.

print(real_id) -- Prints the id itself.

Alright, now that you've got the idea, you just need to put the real_id as the argument for :GetProductInfo, it should auto-matically cast the String into a Number then. Below is an example of this using your script.

Example B

local gui = script.Parent;

local music = workspace.MusicPlayer.MusicScript.Sound;
local SoundId = music.SoundId
local real_id = SoundId:match("%d+")
local Asset = game:GetService("MarketplaceService"):GetProductInfo(real_id)

while wait() do
    gui.Text = "Music name ="..Asset.Name.."/ ID ="..SoundId
end

Also, I'm not sure as to why you are using a while loop but, you must have your reasoning for that.

Well, I hope I helped and have a wonderful day/night.

~~ KingLoneCat

0
Oh someone else got the same idea ^^ arshad145 392 — 6y
Ad
Log in to vote
-1
Answered by
arshad145 392 Moderation Voter
6 years ago
Edited 6 years ago

Trying to help , check it.

Original Explained:

local SoundId = "rbxassetid://787837201" -- You are trying to find an assetId ,not a string.

local Asset = game:GetService("MarketplaceService"):GetProductInfo(rbxassetid://787837201)--This is NOT an assetId.
local Asset1 = game:GetService("MarketplaceService"):GetProducInfo(78737201) -- This is a proper assetId.

while true do
    script.Parent.Text = "Music name ="..Asset.Name.."/ ID = SoundId "
    wait(5)
end


The above is a wrong interpretation of string to Integer.

Corrected one:

local SoundId = "rbxassetid://787837201" -- You are trying to find an assetId not a string.
local real_id = SoundId:match("%d+") -- Matching a pattern.
print(real_id) -- Check output to confirm the Id is the right one.
local Asset = game:GetService("MarketplaceService"):GetProductInfo(real_id)--This gets the real soundId
while true do
    script.Parent.Text = "Music name ="..Asset.Name.."/ ID = SoundId " 
    wait(5)
end

Thank you for reading.

I am still learning RbxLua.

0
there is a script that plays a sound , and i want that sound to show the name and the id of the sound, this is what im trying to do Sphynks 2 — 6y
0
Let me edit my post. arshad145 392 — 6y
0
Who -rep me :( arshad145 392 — 6y

Answer this question