How do I store pcall()'s status code and result separately?

Hello, I was making a function so that when I use HttpService, I don’t need to manually change https:// to http:// when a website doesn’t support it.
However, when using pcall(), the status code is stored but not the result.

Here’s my code:

function getData(ip)
	local data
	local state
	local result
	state, result = pcall(
		function()
			data = HttpService:GetAsync(string.format("https://%s/", ip))
		end
	)
	print(result)
	if tostring() == "false HttpError: SslConnectFail" then
		local data = HttpService:GetAsync(string.format("http://%s/", ip))

	elseif tostring(string.format("%s %s", state, result)) == "false HttpError: ConnectFail" then
		print("Not an HTTP.S server!")

	else
		warn(string.format("Unknown error:\n%s", tostring(result)))
	end

	return data
end

Thank you for reading.

  • You’re missing the arguments to the first tostring
  • it’s probably best to not use tostring at all for error handling and instead check if not state and result == "HttpError: SslConnectFail" then
  • Don’t use local when trying to reassign a variable from an outer scope.
1 Like

you are missing the argument to the tostring() function in this line. you need to pass result as an argument.

function getData(ip)
	local data
	local state
	local result

	state, result = pcall(function()
		data = HttpService:GetAsync(string.format("https://%s/", ip))
	end)

	print(result)

	if tostring(result) == "false HttpError: SslConnectFail" then
		data = HttpService:GetAsync(string.format("http://%s/", ip))
	elseif tostring(string.format("%s %s", state, result)) == "false HttpError: ConnectFail" then
		print("Not an HTTPS server!")
	else
		warn(string.format("Unknown error:\n%s", tostring(result)))
	end

	return data
end
1 Like

for their use case, they would need the parameter to be

string.format("%s %s", state, result)

instead of just

result

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