Skip to content

OnWebEvent

A page of the web interface asked the script something - the value returned is the page’s answer.

The page’s KcdMp.post(name, data) lands here with the frame that called, the name and the data - the JSON the page sent, read as a table, text, number or boolean by the rules of JsonDecode (nil when it sent none). Whatever the callback returns - a table, a text, a number, a boolean or nothing - goes back to the page as JSON, and that is what the page’s promise resolves to.

The page’s call fails instead when the script has no OnWebEvent (the page sees no handler), when the callback raises an error (handler failed; the error text is logged, never sent to the page), when the answer cannot be written as JSON or is over 64 KB, or when no answer comes within about ten seconds. Answer at once: the page waits for the reply, so send anything slow later as a message with SendWebMessage.

Take the page’s data as you would take input from a player: check what is there before using it.

function OnWebEvent(frame, name, data)
-- ...
end
Parameter Type
frame string the frame whose page called
name string what the page called it
data any what the page sent, as a Lua value

the page’s answer: a table, a text, a number, a boolean or nothing

function OnWebEvent(frame, name, data)
if frame == "menu" and name == "buy" and type(data) == "table" and type(data.item) == "string" then
SendServerEvent("shop.buy", JsonEncode({ item = data.item })) -- on to the game mode
return { ok = true }
end
return { ok = false }
end

SendWebMessage · OnWebFrameReady · JsonDecode · the Web interface group of the index