How could i make a script determine if a soundid is invalid, such as if somebody put “ndfsioufnuiondsioun” as the sound id, how would i make a script detect that it isnt a valid sound?
local function ValidateSong(songID)
if not songID or not tonumber(songID) then return false end
local success, result = pcall(function()
return MarketplaceService:GetProductInfo(songID)
end)
if success and result and result.AssetTypeId == 3 then
return true
else
return false
end
end
I wrote this function for this purpose a couple months ago and it serves it’s purpose well. Hopefully it’ll help you out let me know if you have questions.Pretty much what it does is it first checks to make sure that it’s indeed a numerical input that you are getting, next it checks to make s ure that it exists in the “market” or the development catalog, than check if it’s specifically a song if so it returns true.
would you really need to check if its letters though? because it would still not be detected in the market thus being the same as if it were just random numbers?
I guess so but, it prevents the function to go through the trouble to actually validate this and make a marketplaceservice request, so in theory it’s actually faster to check for that initially since you know it’s impossible to have a assetid that isnt numerical.
what if i just use tonumber and if it cant get the tonumber then end the script?
can you put that sentence into code so i can understand what your asking and not assuming.
like this
function playSong(id)
if tonumber(id) then
id = id or ""
if Sound then
Sound:Destroy()
end
Sound = Instance.new'Sound'
Sound.SoundGroup = game:GetService("SoundService"):FindFirstChild("Radios")
Sound.Parent = Handle
Sound.Volume = 0.4
Sound.Looped = true
Sound.PlayOnRemove = false
Sound.SoundId = "http://www.roblox.com/asset/?id="..id
wait(0.5)
if Sound.TimeLength > 0 and Sound.TimeLength <= 2 then
game.Players:GetPlayerFromCharacter(script.Parent.Parent):Kick("Don't play audios less then 2 seconds, this is to prevent crash audios.")
else
Sound:Play()
end
else
end
end
yes, you could do this but there is still potential for it to not be a valid assetid
but if it was then it would still run the script since audios ids are only numbers
Oh yes, it would still run the script obviously, it’s just there is always a potential for it to be an invalid assetid unless you explicitly check for this.
wait i didnt mean what u said u got a point
its just an example of preventing people from trying to play ids with letters, i will add more code to make it validate the id before playing
Sounds good, if you want you can feel free to use the function I provided with you if you want again it does what you are asking it to do. However I digress… I wish you well
would this work?
tonumber(id) and mp:GetProductInfo(id)
yes, that would work as well, but you need to check for the specific assettype, and it might be a good idea to wrap this into a pcall since there is some possibility of this breaking.
im assuming since if it were a wrong is it would go to the else statement and end the script preventing it from playing the audio
If it were a wrong id and you tried to load it into the soundid it would cause an error.
ok so will this work?
function playSong(id)
if tonumber(id) then
local success, result = pcall(function()
return mp:GetProductInfo(id)
end)
if success and result and result.AssetTypeId == 3 then
id = id or ""
if Sound then
Sound:Destroy()
end
Sound = Instance.new'Sound'
Sound.SoundGroup = game:GetService("SoundService"):FindFirstChild("Radios")
Sound.Parent = Handle
Sound.Volume = 0.4
Sound.Looped = true
Sound.PlayOnRemove = false
Sound.SoundId = "http://www.roblox.com/asset/?id="..id
wait(0.5)
if Sound.TimeLength > 0 and Sound.TimeLength <= 2 then
game.Players:GetPlayerFromCharacter(script.Parent.Parent):Kick("Don't play audios less then 2 seconds, this is to prevent crash audios.")
else
Sound:Play()
end
else
end
end
end
What is thi you made if you don’t mine me asking
Script output in Roblox studio, also dev console, this is a no-brainer.