Read through this, the title does not explain exactly what I mean.
I am trying to create a clientsided audio system (Where players can insert an audio ID into a textbox, and play it by clicking the play button. That works perfectly as expected; however, if a code that they enter does not work, it comes back as an error and from there the code stops and does not proceed any further. The goal is to get it to the point in which if the player inserts an invalid ID, it will appear in the textbox for approximately 3 seconds saying “Invalid ID.” I have tried several different techniques, including pcalls (I am a relative amateur to scripting, so I am not yet familiar with pcalls and how they work. Thanks in advance for any help!
I am attaching all 3 different codes that I have developed so far. If the code equals or goes over 5 characters, it works well, however if you insert a single 1, it does not warn of any errors in the textbox.
Code 1 (This code does not play the audio, only shows invalid ID only):
local MarketPlaceService = game:GetService("MarketplaceService")
local id = textbox.Text
local ProductInfo
local function RequestSong()
local success,returnValue = pcall(function()
ProductInfo = MarketPlaceService:GetProductInfo(id)
end)
if success then
script.Parent.Parent.Audio.SoundId = "rbxassetid://"..id
script.Parent.Parent.Audio.Playing = true
end
if not success then
print("Invalid ID.")
textbox.TextColor3 = Color3.new(1, 0, 0)
textbox.Text = "Invalid ID"
wait(3)
textbox.TextColor3 = Color3.new(0, 0, 0)
textbox.Text = ""
end
end
confirmButton.Activated:Connect(function()
RequestSong()
end)
Code 2:
local textbox = script.Parent.AudioID -- textbox path
local remote = game.ReplicatedStorage.IFE -- Remote Event path
local confirmButton = script.Parent.PlayAudio -- confirm button path
local MarketPlaceService = game:GetService("MarketplaceService")
local id = textbox.Text
local success = pcall(function()
MarketPlaceService:GetProductInfo(id)
end)
if success == false then
textbox.Text = "Invalid ID"
end
local id = textbox.Text
local Information = MarketPlaceService:GetProductInfo(id)
if Information.AssetTypeId == 3 then
remote:FireServer(id,Information.Name)
else
textbox.Text = "Invalid Id"
end
Code 3:
local textbox = script.Parent.AudioID -- textbox path
local remote = game.ReplicatedStorage.IFE -- Remote Event path
local confirmButton = script.Parent.PlayAudio -- confirm button path
local function RequestSong()
local id = textbox.Text
print("Recieved the asset ID.")
script.Parent.Parent.Audio.SoundId = "rbxassetid://"..id
print("AssetID has been changed to "..id)
script.Parent.Parent.Audio.Playing = true
if script.Parent.Parent.Audio.SoundId == "Failed to load sound rbxassetid://"..id": Unable to download sound data" then
print("Invalid ID.")
textbox.TextColor3 = Color3.new(1, 0, 0)
textbox.Text = "Invalid ID"
wait(3)
textbox.TextColor3 = Color3.new(0, 0, 0)
textbox.Text = ""
end
end
confirmButton.Activated:Connect(function()
RequestSong()
end)
Please note that these are just renovations that I made to try and make it work. There were different versions in the past, more specifically with the pcall code.
Goal: Whenever a character enters an invalid ID, the textbox displays “Invalid ID” rather than nothing when the error HTTP 400 occurs.
Thanks for any and all help!