diff --git a/functions/Event/cancelEvent.yaml b/functions/Event/cancelEvent.yaml index b82f0d6d..00e28cf3 100644 --- a/functions/Event/cancelEvent.yaml +++ b/functions/Event/cancelEvent.yaml @@ -15,7 +15,7 @@ shared: &shared client: <<: *shared examples: - - path: examples/cancelEvent-2.lua + - path: examples/cancelEvent-1.lua description: This example prevents any damage to a player clientside by making **cancelEvent** an event handler for the [[onClientPlayerDamage]] event. @@ -31,5 +31,5 @@ server: description: The reason for cancelling the event. default: '""' examples: - - path: examples/cancelEvent-1.lua + - path: examples/cancelEvent-2.lua description: This example stops the player from entering a vehicle. \ No newline at end of file diff --git a/functions/Event/cancelLatentEvent.yaml b/functions/Event/cancelLatentEvent.yaml index 18d6c598..b381ff78 100644 --- a/functions/Event/cancelLatentEvent.yaml +++ b/functions/Event/cancelLatentEvent.yaml @@ -17,7 +17,7 @@ shared: &shared server: <<: *shared examples: - - path: examples/cancelLatentEvent-2.lua + - path: examples/cancelLatentEvent-3.lua description: "Cancel all my triggerLatentClientEvent's." client: @@ -27,5 +27,5 @@ client: examples: - path: examples/cancelLatentEvent-1.lua description: '' - - path: examples/cancelLatentEvent-3.lua + - path: examples/cancelLatentEvent-2.lua description: "Cancel all my triggerLatentServerEvent's." diff --git a/functions/Event/examples/addEvent-1.lua b/functions/Event/examples/addEvent-1.lua index 9d3fc1d1..b8a3937d 100644 --- a/functions/Event/examples/addEvent-1.lua +++ b/functions/Event/examples/addEvent-1.lua @@ -1,6 +1,6 @@ addEvent("onSpecialEvent", true) function specialEventHandler(text) - outputChatBox(text) + outputChatBox(text) end addEventHandler("onSpecialEvent", root, specialEventHandler) diff --git a/functions/Event/examples/cancelEvent-1.lua b/functions/Event/examples/cancelEvent-1.lua index 21dbffb1..658ef27d 100644 --- a/functions/Event/examples/cancelEvent-1.lua +++ b/functions/Event/examples/cancelEvent-1.lua @@ -1,4 +1,4 @@ -function onVehicleStartEnter() +function onClientPlayerDamage() cancelEvent() end -addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter) \ No newline at end of file +addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage) \ No newline at end of file diff --git a/functions/Event/examples/cancelEvent-2.lua b/functions/Event/examples/cancelEvent-2.lua index 28e77036..21dbffb1 100644 --- a/functions/Event/examples/cancelEvent-2.lua +++ b/functions/Event/examples/cancelEvent-2.lua @@ -1,4 +1,4 @@ -function onClientPlayerDamage() - cancelEvent() +function onVehicleStartEnter() + cancelEvent() end -addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage) \ No newline at end of file +addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter) \ No newline at end of file diff --git a/functions/Event/examples/cancelLatentEvent-2.lua b/functions/Event/examples/cancelLatentEvent-2.lua index b4099cbb..a3c222dd 100644 --- a/functions/Event/examples/cancelLatentEvent-2.lua +++ b/functions/Event/examples/cancelLatentEvent-2.lua @@ -1,8 +1,8 @@ -addCommandHandler("cancelLatentEvents", function(player) - local handles = getLatentEventHandles(player) -- Returns a table. +addCommandHandler("cancelLatentEvents", function() + local handles = getLatentEventHandles() -- Returns a table. for index = 1, #handles do -- Loop through the table. local handle = handles[index] - cancelLatentEvent(player, handle) -- Cancel it! + cancelLatentEvent(handle) -- Cancel it! end end) diff --git a/functions/Event/examples/cancelLatentEvent-3.lua b/functions/Event/examples/cancelLatentEvent-3.lua index a3c222dd..b4099cbb 100644 --- a/functions/Event/examples/cancelLatentEvent-3.lua +++ b/functions/Event/examples/cancelLatentEvent-3.lua @@ -1,8 +1,8 @@ -addCommandHandler("cancelLatentEvents", function() - local handles = getLatentEventHandles() -- Returns a table. +addCommandHandler("cancelLatentEvents", function(player) + local handles = getLatentEventHandles(player) -- Returns a table. for index = 1, #handles do -- Loop through the table. local handle = handles[index] - cancelLatentEvent(handle) -- Cancel it! + cancelLatentEvent(player, handle) -- Cancel it! end end) diff --git a/functions/Event/examples/triggerClientEvent-1.lua b/functions/Event/examples/triggerClientEvent-1.lua index e8ae5e6e..e7a4093e 100644 --- a/functions/Event/examples/triggerClientEvent-1.lua +++ b/functions/Event/examples/triggerClientEvent-1.lua @@ -7,6 +7,8 @@ addCommandHandler("greet", greetingCommand) -- ***************************************************************************** -- CLIENT CODE -function greetingHandler(message) outputChatBox("The server says: " .. message) end +function greetingHandler(message) + outputChatBox("The server says: " .. message) +end addEvent("onGreeting", true) addEventHandler("onGreeting", localPlayer, greetingHandler) diff --git a/functions/Event/examples/triggerEvent-1.lua b/functions/Event/examples/triggerEvent-1.lua index 338c059d..b2e0d8fb 100644 --- a/functions/Event/examples/triggerEvent-1.lua +++ b/functions/Event/examples/triggerEvent-1.lua @@ -1,5 +1,5 @@ function onCustomEvent(chatMessage) - outputChatBox(chatMessage) + outputChatBox(chatMessage) end addEvent("onCustomEvent", false) -- set to false, so this event won't be called from counter side - important security measure addEventHandler("onCustomEvent", root, onCustomEvent) diff --git a/functions/Event/examples/triggerServerEvent-1.lua b/functions/Event/examples/triggerServerEvent-1.lua index 19f9245d..ba70c99f 100644 --- a/functions/Event/examples/triggerServerEvent-1.lua +++ b/functions/Event/examples/triggerServerEvent-1.lua @@ -1,47 +1,47 @@ -- ***************************************************************************** -- CLIENT CODE function sendMessageToServer() - local messageToSend = "Hello, world!" -- this string would be passed to server + local messageToSend = "Hello, world!" -- this string would be passed to server - triggerServerEvent("onServerSendMessage", localPlayer, messageToSend) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argument + triggerServerEvent("onServerSendMessage", localPlayer, messageToSend) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argument end addCommandHandler("message", sendMessageToServer) -- ***************************************************************************** -- SERVER CODE function onServerSendMessage(sentMessage) - if (not client) then -- 'client' points to the player who triggered the event, and should be used as security measure (in order to prevent player faking) - return false -- if this variable doesn't exists at the moment (for unknown reason, or it was the server who triggered this event), stop code execution - end + if (not client) then -- 'client' points to the player who triggered the event, and should be used as security measure (in order to prevent player faking) + return false -- if this variable doesn't exists at the moment (for unknown reason, or it was the server who triggered this event), stop code execution + end - local matchingSource = (source == client) -- check whether source element (2nd argument in triggerServerEvent) passed from client was the exact same player + local matchingSource = (source == client) -- check whether source element (2nd argument in triggerServerEvent) passed from client was the exact same player - if (not matchingSource) then -- apparently it wasn't - return false -- so do not process this event - end + if (not matchingSource) then -- apparently it wasn't + return false -- so do not process this event + end - local dataType = type(sentMessage) -- check type of data coming from client - local dataString = (dataType == "string") -- check whether it's string + local dataType = type(sentMessage) -- check type of data coming from client + local dataString = (dataType == "string") -- check whether it's string - if (not dataString) then -- if it isn't string - return false -- stop our code here - end + if (not dataString) then -- if it isn't string + return false -- stop our code here + end - local minStringLength = 1 -- min allowed length of string - local maxStringLength = 64 -- max allowed length of string - local stringLength = utf8.len(sentMessage) -- get string length - local allowedStringLength = (stringLength >= minStringLength and stringLength <= maxStringLength) -- verify whether length is allowed + local minStringLength = 1 -- min allowed length of string + local maxStringLength = 64 -- max allowed length of string + local stringLength = utf8.len(sentMessage) -- get string length + local allowedStringLength = (stringLength >= minStringLength and stringLength <= maxStringLength) -- verify whether length is allowed - if (not allowedStringLength) then -- if string length was exceeded - return false -- tell server to stop here - end + if (not allowedStringLength) then -- if string length was exceeded + return false -- tell server to stop here + end - local playerName = getPlayerName(client) -- get name of player who sent message - local chatMessage = playerName.." sent message from client: "..sentMessage + local playerName = getPlayerName(client) -- get name of player who sent message + local chatMessage = playerName.." sent message from client: "..sentMessage - outputChatBox(chatMessage, root, 255, 255, 255, false) -- output text sent from client-side for everyone on server + outputChatBox(chatMessage, root, 255, 255, 255, false) -- output text sent from client-side for everyone on server - -- useful utility for checking event data: https://wiki.multitheftauto.com/wiki/Script_security + -- useful utility for checking event data: https://wiki.multitheftauto.com/wiki/Script_security end addEvent("onServerSendMessage", true) -- 2nd argument should be set to true, in order to be triggered from counter side (in this case client-side) addEventHandler("onServerSendMessage", root, onServerSendMessage) \ No newline at end of file diff --git a/functions/Event/examples/triggerServerEvent-2.lua b/functions/Event/examples/triggerServerEvent-2.lua index 4bb3f90b..d9d87cfb 100644 --- a/functions/Event/examples/triggerServerEvent-2.lua +++ b/functions/Event/examples/triggerServerEvent-2.lua @@ -1,64 +1,64 @@ -- ***************************************************************************** -- CLIENT CODE function sendTableToServer() - local tableToSent = {1, 2, 3} -- this table would be passed to server + local tableToSent = {1, 2, 3} -- this table would be passed to server - triggerServerEvent("onServerSendTable", resourceRoot, tableToSent) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argument + triggerServerEvent("onServerSendTable", resourceRoot, tableToSent) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argument end addCommandHandler("table", sendTableToServer) -- ***************************************************************************** -- SERVER CODE function onServerSendTable(sentTable) - if (not client) then -- 'client' points to the player who triggered the event, and should be used as security measure (in order to prevent player faking) - return false -- if this variable doesn't exists at the moment (for unknown reason, or it was the server who triggered this event), stop code execution - end + if (not client) then -- 'client' points to the player who triggered the event, and should be used as security measure (in order to prevent player faking) + return false -- if this variable doesn't exists at the moment (for unknown reason, or it was the server who triggered this event), stop code execution + end - local matchingSource = (source == resourceRoot) -- check whether source element (2nd argument in triggerServerEvent) passed from client was resourceRoot + local matchingSource = (source == resourceRoot) -- check whether source element (2nd argument in triggerServerEvent) passed from client was resourceRoot - if (not matchingSource) then -- apparently it wasn't - return false -- so do not process this event - end + if (not matchingSource) then -- apparently it wasn't + return false -- so do not process this event + end - local dataType = type(sentTable) -- check type of data coming from client - local dataTable = (dataType == "table") -- check whether it's table + local dataType = type(sentTable) -- check type of data coming from client + local dataTable = (dataType == "table") -- check whether it's table - if (not dataTable) then -- if it isn't table - return false -- stop our code here - end + if (not dataTable) then -- if it isn't table + return false -- stop our code here + end - local minTableLength = 1 -- min allowed length of table - local maxTableLength = 3 -- max allowed length of table - local tableLength = (#sentTable) -- get table length - local matchingTableLength = (tableLength >= minTableLength and tableLength <= maxTableLength) -- verify whether length is allowed + local minTableLength = 1 -- min allowed length of table + local maxTableLength = 3 -- max allowed length of table + local tableLength = (#sentTable) -- get table length + local matchingTableLength = (tableLength >= minTableLength and tableLength <= maxTableLength) -- verify whether length is allowed - if (not matchingTableLength) then -- if table length was exceeded - return false -- tell server to stop here - end + if (not matchingTableLength) then -- if table length was exceeded + return false -- tell server to stop here + end - local numbersOnly = true -- variable which will hold whether numbers only were in the table + local numbersOnly = true -- variable which will hold whether numbers only were in the table - for _, numberValue in pairs(sentTable) do -- iterate through table - local valueType = type(numberValue) -- check type of each data - local numberType = (valueType == "number") -- check whether it's a number + for _, numberValue in pairs(sentTable) do -- iterate through table + local valueType = type(numberValue) -- check type of each data + local numberType = (valueType == "number") -- check whether it's a number - if (not numberType) then -- it isn't a number - numbersOnly = false -- since at least one of data inside isn't number, set it to false - break -- break the loop, no need to check further - end - end + if (not numberType) then -- it isn't a number + numbersOnly = false -- since at least one of data inside isn't number, set it to false + break -- break the loop, no need to check further + end + end - if (not numbersOnly) then -- it isn't numbers only table - return false -- stop code execution - end + if (not numbersOnly) then -- it isn't numbers only table + return false -- stop code execution + end - local playerName = getPlayerName(client) -- get name of player who sent message - local sentNumbers = table.concat(sentTable, ", ") -- convert our table into list of numbers separated by comma - local chatMessage = playerName.." sent table with numbers from client: "..sentNumbers + local playerName = getPlayerName(client) -- get name of player who sent message + local sentNumbers = table.concat(sentTable, ", ") -- convert our table into list of numbers separated by comma + local chatMessage = playerName.." sent table with numbers from client: "..sentNumbers - outputChatBox(chatMessage, root, 255, 255, 255, false) -- output numbers sent from client-side for everyone on server + outputChatBox(chatMessage, root, 255, 255, 255, false) -- output numbers sent from client-side for everyone on server - -- useful utility for checking event data: https://wiki.multitheftauto.com/wiki/Script_security + -- useful utility for checking event data: https://wiki.multitheftauto.com/wiki/Script_security end addEvent("onServerSendTable", true) -- 2nd argument should be set to true, in order to be triggered from counter side (in this case client-side) addEventHandler("onServerSendTable", resourceRoot, onServerSendTable) \ No newline at end of file