-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathracetimer.lua
More file actions
123 lines (100 loc) · 3.06 KB
/
racetimer.lua
File metadata and controls
123 lines (100 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
--Copyright (c) 2022 Caitex#6714. Please read the "L-License": https://github.com/Caitex212/lua-examples/blob/main/L-License.txt
--Countdown example
local commandPrefix = "/"
local raceallTime
local admin = {"Caitex.212"}
-- Register the Events in the Server...
function onInit()
print("----------Loading Racing Script Done!----------")
MP.RegisterEvent("onChatMessage","onChatMessageHandler")
print("Register race command!")
MP.RegisterEvent("race", "raceCommand")
MP.CreateEventTimer("raceallTimer",1000)
end
--Receiving the messages and sending them to the onCommand function.
function onChatMessageHandler(player_id, player_name, message)
print("onChatMessage: " .. player_id .. " | " .. player_name .. " | Message: " .. message)
--Looking for the commandPrefix.
message = message:sub(2)
if message:sub(1,1) == commandPrefix then
command = string.sub(message,2)
onCommand(player_id, command)
return
end
end
function onCommand(player_id, data)
local command = split(data," ")[1]
local args
--Splitting the message into different "args".
local s = data:find(' ')
if s ~= nil then
args = data:sub(s+1)
end
args = args or ""
--Triggering the local function.
if command == "race" then
raceCommand(args, player_id)
else
MP.TriggerLocalEvent(command, args)
end
end
function raceCommand(args, player_id)
print("|"..args.."|", player_id)
if args == "" then
--Checking if the player is an admin.
local isAdmin = false
for key, value in pairs(admin) do
if MP.GetPlayerName(player_id) == value then
isAdmin = true
break
end
end
if isAdmin then
--Set the time so the countdown can start.
raceallTime = 11
else
MP.SendChatMessage(player_id, "You cannot use that command!")
end
end
end
--This function is triggered every second, and sends the countdown.
function raceallTimer()
if raceallTime ~= nil then
if raceallTime == 11 then
MP.SendChatMessage(-1, "Race Starting In")
elseif raceallTime == 10 then
MP.SendChatMessage(-1, "10")
elseif raceallTime == 9 then
MP.SendChatMessage(-1, "9")
elseif raceallTime == 8 then
MP.SendChatMessage(-1, "8")
elseif raceallTime == 7 then
MP.SendChatMessage(-1, "7")
elseif raceallTime == 6 then
MP.SendChatMessage(-1, "6")
elseif raceallTime == 5 then
MP.SendChatMessage(-1, "5")
elseif raceallTime == 4 then
MP.SendChatMessage(-1, "4")
elseif raceallTime == 3 then
MP.SendChatMessage(-1, "3")
elseif raceallTime == 2 then
MP.SendChatMessage(-1, "2")
elseif raceallTime == 1 then
MP.SendChatMessage(-1, "1")
elseif raceallTime == 0 then
MP.SendChatMessage(-1, "GO")
end
raceallTime = raceallTime - 1
if raceallTime == -1 then
raceallTime = nil
end
end
end
function split(s, sep)
local fields = {}
local sep = sep or " "
local pattern = string.format("([^%s]+)", sep)
string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
return fields
end