로블록스 게임제작

pcall

Dean83 2024. 2. 14. 09:46

보통 try catch를 쓰는것과 다르게 lua script 에서는 pcall 이라는것이 이를 대신한다. 

httpservice 를 통해 RESTAPI 통신할때 사용하면 유용하다. 

lua script 에서는 리턴값을 여러개 받을 수 있는 모양이다. pcall 을 이용하면 성공여부 (try catch 성공여부) 및 결과값 리턴을 동시에 해준다.

 

local success, response = pcall(function()
    return HttpService:PostAsync(url, HttpService:JSONEncode(requestBody))
end)

if success then
    if response.StatusCode == 400 then
        print("Bad request")
    else
        print("Request was successful")
    end
else
    print("Request failed with error:", response)
end