Is there a way to create an if error then statement?

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!

image

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!

I think the cause of code 1 not working is that you are not “calling the if statements”. Try putting it after Request Song in the confirmButton.Activated, so that it actually checks, because in my opinion the if statements don’t actually run.

Edit:
Put the if statements inside the request song?
Maybe try to convert the string to a number with tonumber()?
Also are there any errors, if so please list them.

That doesn’t work, it does the same thing (Shows “Invalid ID” whether it’s valid or not). This is the audio ID that I’m using to test it (it’s just the first audio that was in the toolbox)

6120602650

EDIT: This is what it’s supposed to show when the ID is in fact invalid
image

Regarding code 1:

success is locally defined in your RequestSong() function so it won’t be accessible to the code running below it. I’d expect an error normally on your if success then line, but you must have success defined outside of RequestSong() above somewhere. Regardless, this is why it says “Invalid ID” every time in your Code 1.


Regarding Code 2:

Everything below the if success == false code is going to run because you didn’t put it into an else block. You also seem to be repeating yourself down below. What was the result of this code though?


Regarding Code 3:

To my knowledge, this behavior does not happen when an audio fails to load. However, you can listen for the error messages that appear when a sound fails to load (Ex: Failed to load sound rbxassetid://213275044: Unable to download sound data). To do this you’ll need to use LogService.MessageOut. I personally used this in one of my games.

FYI:

You can directly connect the function rather than calling it like this:

confirmButton.Activated:Connect(RequestSong)

Put the if statements inside the function, and if that doesn’t work, make it a global variable.
Try:

local MarketPlaceService = game:GetService(“MarketplaceService”)
local id = tonumber(textbox.Text)
local id2 = “rbxassetid://”…id
local ProductInfo = “”

local function RequestSong()
local success = pcall(function()
return ProductInfo = MarketPlaceService:GetProductInfo(id)
end)
if success then
script.Parent.Parent.Audio.SoundId = id2
script.Parent.Parent.Audio.Playing = true
elseif 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)

Edit:
Also, do you have the part the defines what “Textbox” is, because it doesn’t show where textbox is defined…

I am trying this now. The audio plays, but Invalid ID still shows in the textbox. I am trying a few things to work around it and see if I can get it to work, but is there anything specific about LogService:MessageOut that I need to know?

EDIT: It keeps looping increasing the amount of “Invalid IDs” in the output each time I run the audio. Here’s my code as of now:

local textbox = script.Parent.AudioID -- textbox path
local remote = game.ReplicatedStorage.IFE -- Remote Event path
local confirmButton = script.Parent.PlayAudio -- confirm button path

confirmButton.Activated:Connect(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
	local msg = Instance.new("Message", workspace)
	game:GetService("LogService").MessageOut:Connect(function(Message, Type)
		msg.Text = "The message was "..Message.." and the type was "..tostring(Type)
		msg:Destroy()
		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)

Thanks for your help and I’m so sorry if this is something simple, haha.

Thats where it’s defined, are you looking at code 1?

EDIT: My bad, forgot to include the entire top section of the code:

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 ProductInfo

I am looking at code one, does it give errors for it?
other than invalid id…
Also, ps, make sure to reduce the code … to double dots, not triple.
Roblox auto corrects it…

Code 1 is a blank. Nothing pulls up for it. Trying your code now.

This code is also a blank, nothing is pulling up for it.

Your first code snippet doesn’t work because you prematurely assigned the ID variable to the TextBox’s text.

local MarketPlaceService = game:GetService("MarketplaceService")
local ProductInfo

local function RequestSong()
	local id = tonumber(textbox.Text); --It should be defined here instead
	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)

To explain, when you reference an object’s property which is either a number or a string, you get the value of that property. Meaning if you change that property after assigning a variable to it, the variable will not change.

local textbox = somethingOrOther;
textbox.Text = "test";

local text1 = textbox.Text;
print(text1) --> "test";

textbox.Text = "something else";
print(text1); --> "test"

It just displays invalid id?
To trouble shoot, print all the variables.
I know this sounds really basic, but hey, thats how I trouble shoot…

I think he did this because he needs a way to detect the textbox, and if you don’t have it you cannot necessarily input the id. I have seen games who use this but with decals.

This code works well, but it reverses the original problem (It shows invalid ID for numbers below 5-6 characters), and simply just sends an error to output for numbers above 5-6 characters (Thus no longer firing the code)

Would there be a way that I can check to see if it is a certain AssetTypeId? The AssetTypeId has to be 3 (Audio), and if that is not the case with an ID they enter, make Invalid ID appear?

The dictionary returned by :GetProductInfo() contains an AssetTypeId, which will be 3 if it’s an audio.

local MarketPlaceService = game:GetService("MarketplaceService")
local ProductInfo

local function RequestSong()
	local id = tonumber(textbox.Text); --It should be defined here instead
	local success,returnValue = pcall(function()
		ProductInfo = MarketPlaceService:GetProductInfo(id)
	end)

	if success and ProductInfo.AssetTypeId == 3 then
		script.Parent.Parent.Audio.SoundId = "rbxassetid://"..id
		script.Parent.Parent.Audio.Playing = true
	else
		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)

That works as expected! Thanks for your help!

1 Like