Weird pcall error (did not catch error [kinda]) [bug?]

I have my code here that errors in line 70 and pointing at end when i run this at the commandbar
Note: _success didn’t get printed

require(game.StarterGui.NPC_Dialogue.ModuleScript):DisplayText("Bruh", {{["ActionText"] = 'Interact', ["KeyCode"] = Enum.KeyCode.N}, {["ActionText"] = "BRUH?", ["KeyCode"] = Enum.KeyCode.ButtonA}})

Here’s the rest of the code:

--# selene: allow(unused_variable)
type void = nil
local void = nil

	local RepStor = game:GetService('ReplicatedStorage')
	--local BFM = require(game.ReplicatedStorage.BasicFunctionModule)
	local framer = script.Parent.Framer
	local speechText = framer.SpeechText
	local decisions = framer.Decisions

local main = {} do
	main.__index = {}
	function main:DisplayText(
		text: string,
		options: {
			[number]: {
				["Text"]: string,
				["KeyCode"]: Enum.KeyCode,
				["ActionText"]: string
		text = tostring(text); assert(text ~= nil, "Impossible text string");
		local guiObject = speechText
		local function typeWrite(delayBetweenChars: number) -- from ``
			guiObject.Visible = true
			guiObject.AutoLocalize = false
			local displayText = text

			-- Replace line break tags so grapheme loop will not miss those characters
			displayText = displayText:gsub("<br%s*/>", "\n")
			-- Remove RichText tags since char-by-char animation will break the tags
			displayText = displayText:gsub("<[^<>]->", "")

			-- Set translated/modified text on parent
			guiObject.Text = displayText

			local index = 0

			local function calculateDelay(character)
				return (character:match("%p") and delayBetweenChars * 2) or delayBetweenChars 

			for first, last in utf8.graphemes(displayText) do
				index += 1
				guiObject.MaxVisibleGraphemes = index
					calculateDelay(displayText:sub(last, last))
		--assert(options, "No options given")
		if not options or type(options) ~= "table" then
		local function clearTextButtons()
			for _, child in (decisions:GetChildren()) do
				if child:IsA('TextButton') then
				end -- <- Line 70
		for i in (options) do			
			local cloneBtn = script.DecisionA:Clone()
			cloneBtn.Name = options[i].ActionText
			local _success, keyCodeName = pcall(string.char, options[i].KeyCode.Value)
			if not _success then
				keyCodeName = options[i].KeyCode.Name
				--RightControl -> RightCtrl
				local dir = keyCodeName:match("(.-)Control$")
				if dir then
					keyCodeName = dir.. "Ctrl"

				--ButtonX -> X
				local gPadButton = keyCodeName:match('^Button(.-)')
				if gPadButton then
					keyCodeName = gPadButton
				keyCodeName = keyCodeName:upper()
			cloneBtn.KeyCodeLabel.Text, cloneBtn.ActionLabel.Text = `[{keyCodeName}]`, options[i].ActionText
			cloneBtn.Parent = decisions
			cloneBtn.LayoutOrder = i

return main;

Thank you for your time


What is string.char?


string.char() is a Lua function that converts a list of integers to their corresponding characters in the ASCII table.

For example:

print(string.char(65))  -- Output: A
print(string.char(97))  -- Output: a

In this example, string.char(65) returns the character “A”, and string.char(97) returns the character “a”, as these are the ASCII representations of those numbers.

print("\65") -- Output: A
print("\97") -- Output: a

I put this in a pcall because it might not have an ascii representation.

So options[i].KeyCode.Value is an integer you’re passing to string.char()? Bc I think the error is pointing out that it’s an invalid argument.

That’s why I put it in a pcall.

You put it in a pcall so that it would catch the invalid argument that you passed in?

I wouldn’t say that I put in an invalid argument, I think the argument (that is a number) might not have a ASCII representation.

Like this.

  21:33:22.461  > print(string.char(999))  -  Studio
  21:33:22.462  print(string.char(999)):1: invalid argument #1 to 'char' (invalid value)

Just restarted Roblox Studio and…

The Exception hit moved to line 79

So, are you looking for a way to handle the numbers that are not covered by ascii?

Yes, if the number is not covered by an ASCII representation then just use it’s name.

I believe this is because you have a debugger attached with catch all exceptions instead of only ones that are not handled, try stepping over and see if you can run any other code

I don’t have any debugger attached and it’s an exception hit, so every other code ran normally.

you have a debugger attached bro, you are debugging the code using the studio debugger that’s why you have that error UI and you go to your script and everything stops.

If you didn’t you would just get console output, show me your top bar of studio when you are inside of the script when erroring

Verbatim, I don’t have any debugger of sorts that is enabled…
The debugger options are greyed out so I can’t use it (don’t tell me how to use the debugger).


  21:59:51.091  > require(game.StarterGui.NPC_Dialogue.ModuleScript):DisplayText("Bruh", {[1] = {["ActionText"] = 'Interact', ["KeyCode"] = Enum.KeyCode.N}, [2] = {["ActionText"] = "BRUH?", ["KeyCode"] = Enum.KeyCode.ButtonA}})  -  Studio
  21:59:51.092  true  -  Studio
  21:59:51.093  invalid argument #1 to 'char' (invalid value)  -  Studio
  21:59:51.093  Stack Begin  -  Studio
  21:59:51.093  Script 'StarterGui.NPC_Dialogue.ModuleScript', Line 79 - function DisplayText  -  Studio
  21:59:51.093  Script 'require(game.StarterGui.NPC_Dialogue.ModuleScript):DisplayText("Bruh", {[1] = {["ActionText"] = 'Interact', ["KeyCode"] = Enum.KeyCode.N}, [2] = {["ActionText"] = "BRUH?", ["KeyCode"] = Enum.KeyCode.ButtonA}})', Line 1  -  Studio
  21:59:51.093  Stack End  -  Studio

i guess try doing

pcall(function() end)

instead of just calling pcall(string.char)

Instead of relying on pcall to catch the error, you could instead write your own function to check if the number is greater than 9, in which case you’d use the name.

I don’t think this works when I run it in the Command Bar.
But when I do it on play test, it doesn’t error anything and everything works fine.

Updated code:

local _success, keyCodeName
			local function asciiRepresent()
				_success, keyCodeName = pcall(string.char, option.KeyCode.Value)
				if not _success then
					keyCodeName = option.KeyCode.Name
					--RightControl -> RightCtrl
					local dir = keyCodeName:match("(.-)Control$")
					if dir then
						keyCodeName = dir.. " Ctrl"
					--ButtonX -> X
					local gPadButton = keyCodeName:match('^Button(.+)')
					if gPadButton then
						keyCodeName = gPadButton
					keyCodeName = (`{keyCodeName}`):upper()


  22:56:55.604  N  -  Server - ModuleScript:105
  22:56:55.604  A  -  Server - ModuleScript:105
-----Current Solution: don't run it in the Command Bar

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.