Fixes for Lua code examples in event functions by omar-o22 · Pull Request #53 · multitheftauto/wiki.multitheftauto.com · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions functions/Event/cancelEvent.yaml
4 changes: 2 additions & 2 deletions functions/Event/cancelLatentEvent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."
2 changes: 1 addition & 1 deletion functions/Event/examples/addEvent-1.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
addEvent("onSpecialEvent", true)

function specialEventHandler(text)
outputChatBox(text)
outputChatBox(text)
end
addEventHandler("onSpecialEvent", root, specialEventHandler)
4 changes: 2 additions & 2 deletions functions/Event/examples/cancelEvent-1.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function onVehicleStartEnter()
function onClientPlayerDamage()
cancelEvent()
end
addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter)
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)
6 changes: 3 additions & 3 deletions functions/Event/examples/cancelEvent-2.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function onClientPlayerDamage()
cancelEvent()
function onVehicleStartEnter()
cancelEvent()
end
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)
addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter)
6 changes: 3 additions & 3 deletions functions/Event/examples/cancelLatentEvent-2.lua
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions functions/Event/examples/cancelLatentEvent-3.lua
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion functions/Event/examples/triggerClientEvent-1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion functions/Event/examples/triggerEvent-1.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
50 changes: 25 additions & 25 deletions functions/Event/examples/triggerServerEvent-1.lua
Original file line number Diff line number Diff line change
@@ -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)
76 changes: 38 additions & 38 deletions functions/Event/examples/triggerServerEvent-2.lua
Loading