Handling errors that you know will occur

Handling errors that you know will occur but are user made, I am working on a local script that changes the sound Id but once it does, an error could occur if the sound id was not input correctly, this being a user input mistake.

        game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('Settings'):WaitForChild('musictab'):WaitForChild('PLAY'):WaitForChild('TextButton').Activated:Connect(function()
	if debounce7 == false then	
		debounce7 = true
		
		local f = coroutine.resume(coroutine.create(function()
			defaults = false
			Audio.TimePosition = 0
			Audio.SoundId = 'rbxassetid://'..game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('Settings'):WaitForChild('musictab'):WaitForChild('musicid'):WaitForChild('TextBox').Text
			Audio.Playing = true
			wait(1)
			debounce7 = false
		end))
				
		local suc, err = pcall(f)
		
		if not suc then
			Audio:Destroy()
			local g = Instance.new('Sound')
			g.Parent = game.ReplicatedStorage
			g.Name = 'Sound'
			g.Volume = 0.05
			
			defaults = true
			debounce7 = false
			print(err)
			wait(1)
		else
			defaults = false
			Audio.TimePosition = 0
			Audio.SoundId = 'rbxassetid://'..game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('Settings'):WaitForChild('musictab'):WaitForChild('musicid'):WaitForChild('TextBox').Text
			Audio.Playing = true
			wait(1)
			debounce7 = false
		end
	end
end)

I am unsure of how to handle if you typed the right value in or not, I used pcalls but to no avail did it work, I could be using them wrong but could someone point me in the right direction ?

pcall the function directly. There’s no need to wrap it in a coroutine. It will run before you pcall it, since it runs when you .resume the coroutine.

2 Likes

I still get the same error, but I should clarify more my problem. When I type in the correct audio ID with just the body it works.

		defaults = false
		Audio.TimePosition = 0
		Audio.SoundId = 'rbxassetid://'..game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('Settings'):WaitForChild('musictab'):WaitForChild('musicid'):WaitForChild('TextBox').Text
		Audio.Playing = true
		wait(1)
		debounce7 = false

But I need a pcall to catch any errors such as typing the ID wrong or in general the audio ID is invalid. When I wrap the function with a pcall without the coroutine like you suggested, it still always returns false implying that there’s still something wrong with what i’m doing, maybe an example could help point me a bit toward what i’m trying to do?

Maybe instead of trying to pcall it, you could just check if it’s all numeric? Try something with string patterns, like this:

local playersText = game.Players.LocalPlayer:WaitForChild -- and so on
Audio.SoundId = "rbxassetid://" .. (playersText:match("%d+") or "") -- %d will match digits and return nil if there are none
2 Likes

This still would not work because it would error if the numeric value is wrong

I don’t know if you figured this out by now or not, but I’m curious what line of your script is throwing the error? I am curious because I made my own player and all I did was assign the user’s input (bogus or not) to the rbxassetid, then my script attempts to play it with the sound:Play() method. I then check the ‘loaded’ status for for a few cycles(maybe one second) and if it doesn’t load, I consider it bogus and move on with other options. It never throws an error message or stops the script.