Skip to content

Commit d55fce0

Browse files
authored
Fixes for Lua code examples in event functions (#53)
1 parent 28a23a9 commit d55fce0

11 files changed

+83
-81
lines changed

functions/Event/cancelEvent.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ shared: &shared
1515
client:
1616
<<: *shared
1717
examples:
18-
- path: examples/cancelEvent-2.lua
18+
- path: examples/cancelEvent-1.lua
1919
description: This example prevents any damage to a player clientside by making **cancelEvent** an
2020
event handler for the [[onClientPlayerDamage]] event.
2121

@@ -31,5 +31,5 @@ server:
3131
description: The reason for cancelling the event.
3232
default: '""'
3333
examples:
34-
- path: examples/cancelEvent-1.lua
34+
- path: examples/cancelEvent-2.lua
3535
description: This example stops the player from entering a vehicle.

functions/Event/cancelLatentEvent.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ shared: &shared
1717
server:
1818
<<: *shared
1919
examples:
20-
- path: examples/cancelLatentEvent-2.lua
20+
- path: examples/cancelLatentEvent-3.lua
2121
description: "Cancel all my triggerLatentClientEvent's."
2222

2323
client:
@@ -27,5 +27,5 @@ client:
2727
examples:
2828
- path: examples/cancelLatentEvent-1.lua
2929
description: ''
30-
- path: examples/cancelLatentEvent-3.lua
30+
- path: examples/cancelLatentEvent-2.lua
3131
description: "Cancel all my triggerLatentServerEvent's."
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
addEvent("onSpecialEvent", true)
22

33
function specialEventHandler(text)
4-
outputChatBox(text)
4+
outputChatBox(text)
55
end
66
addEventHandler("onSpecialEvent", root, specialEventHandler)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function onVehicleStartEnter()
1+
function onClientPlayerDamage()
22
cancelEvent()
33
end
4-
addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter)
4+
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function onClientPlayerDamage()
2-
cancelEvent()
1+
function onVehicleStartEnter()
2+
cancelEvent()
33
end
4-
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)
4+
addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
addCommandHandler("cancelLatentEvents", function(player)
2-
local handles = getLatentEventHandles(player) -- Returns a table.
1+
addCommandHandler("cancelLatentEvents", function()
2+
local handles = getLatentEventHandles() -- Returns a table.
33

44
for index = 1, #handles do -- Loop through the table.
55
local handle = handles[index]
6-
cancelLatentEvent(player, handle) -- Cancel it!
6+
cancelLatentEvent(handle) -- Cancel it!
77
end
88
end)
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
addCommandHandler("cancelLatentEvents", function()
2-
local handles = getLatentEventHandles() -- Returns a table.
1+
addCommandHandler("cancelLatentEvents", function(player)
2+
local handles = getLatentEventHandles(player) -- Returns a table.
33

44
for index = 1, #handles do -- Loop through the table.
55
local handle = handles[index]
6-
cancelLatentEvent(handle) -- Cancel it!
6+
cancelLatentEvent(player, handle) -- Cancel it!
77
end
88
end)

functions/Event/examples/triggerClientEvent-1.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ addCommandHandler("greet", greetingCommand)
77

88
-- *****************************************************************************
99
-- CLIENT CODE
10-
function greetingHandler(message) outputChatBox("The server says: " .. message) end
10+
function greetingHandler(message)
11+
outputChatBox("The server says: " .. message)
12+
end
1113
addEvent("onGreeting", true)
1214
addEventHandler("onGreeting", localPlayer, greetingHandler)

functions/Event/examples/triggerEvent-1.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function onCustomEvent(chatMessage)
2-
outputChatBox(chatMessage)
2+
outputChatBox(chatMessage)
33
end
44
addEvent("onCustomEvent", false) -- set to false, so this event won't be called from counter side - important security measure
55
addEventHandler("onCustomEvent", root, onCustomEvent)
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
-- *****************************************************************************
22
-- CLIENT CODE
33
function sendMessageToServer()
4-
local messageToSend = "Hello, world!" -- this string would be passed to server
4+
local messageToSend = "Hello, world!" -- this string would be passed to server
55

6-
triggerServerEvent("onServerSendMessage", localPlayer, messageToSend) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argument
6+
triggerServerEvent("onServerSendMessage", localPlayer, messageToSend) -- refer to the note on wiki page (under theElement), for understanding which element you should use as 2nd argument
77
end
88
addCommandHandler("message", sendMessageToServer)
99

1010
-- *****************************************************************************
1111
-- SERVER CODE
1212
function onServerSendMessage(sentMessage)
13-
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)
14-
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
15-
end
13+
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)
14+
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
15+
end
1616

17-
local matchingSource = (source == client) -- check whether source element (2nd argument in triggerServerEvent) passed from client was the exact same player
17+
local matchingSource = (source == client) -- check whether source element (2nd argument in triggerServerEvent) passed from client was the exact same player
1818

19-
if (not matchingSource) then -- apparently it wasn't
20-
return false -- so do not process this event
21-
end
19+
if (not matchingSource) then -- apparently it wasn't
20+
return false -- so do not process this event
21+
end
2222

23-
local dataType = type(sentMessage) -- check type of data coming from client
24-
local dataString = (dataType == "string") -- check whether it's string
23+
local dataType = type(sentMessage) -- check type of data coming from client
24+
local dataString = (dataType == "string") -- check whether it's string
2525

26-
if (not dataString) then -- if it isn't string
27-
return false -- stop our code here
28-
end
26+
if (not dataString) then -- if it isn't string
27+
return false -- stop our code here
28+
end
2929

30-
local minStringLength = 1 -- min allowed length of string
31-
local maxStringLength = 64 -- max allowed length of string
32-
local stringLength = utf8.len(sentMessage) -- get string length
33-
local allowedStringLength = (stringLength >= minStringLength and stringLength <= maxStringLength) -- verify whether length is allowed
30+
local minStringLength = 1 -- min allowed length of string
31+
local maxStringLength = 64 -- max allowed length of string
32+
local stringLength = utf8.len(sentMessage) -- get string length
33+
local allowedStringLength = (stringLength >= minStringLength and stringLength <= maxStringLength) -- verify whether length is allowed
3434

35-
if (not allowedStringLength) then -- if string length was exceeded
36-
return false -- tell server to stop here
37-
end
35+
if (not allowedStringLength) then -- if string length was exceeded
36+
return false -- tell server to stop here
37+
end
3838

39-
local playerName = getPlayerName(client) -- get name of player who sent message
40-
local chatMessage = playerName.." sent message from client: "..sentMessage
39+
local playerName = getPlayerName(client) -- get name of player who sent message
40+
local chatMessage = playerName.." sent message from client: "..sentMessage
4141

42-
outputChatBox(chatMessage, root, 255, 255, 255, false) -- output text sent from client-side for everyone on server
42+
outputChatBox(chatMessage, root, 255, 255, 255, false) -- output text sent from client-side for everyone on server
4343

44-
-- useful utility for checking event data: https://wiki.multitheftauto.com/wiki/Script_security
44+
-- useful utility for checking event data: https://wiki.multitheftauto.com/wiki/Script_security
4545
end
4646
addEvent("onServerSendMessage", true) -- 2nd argument should be set to true, in order to be triggered from counter side (in this case client-side)
4747
addEventHandler("onServerSendMessage", root, onServerSendMessage)

0 commit comments

Comments
 (0)