-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
380 lines (331 loc) · 12.5 KB
/
client.lua
File metadata and controls
380 lines (331 loc) · 12.5 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
-- client.lua
-- =====================================
-- CONFIGURATION
-- =====================================
Config = {}
-- Define items available in the game
Config.Items = {
{ itemId = 1, name = "Health Kit" },
{ itemId = 2, name = "Armor" },
{ itemId = 3, name = "Bandage" },
-- Add more items as needed
}
-- Define Ammu-Nation store locations
Config.AmmuNationStores = {
{ x = 452.6, y = -980.0, z = 30.7 },
{ x = 1693.4, y = 3760.2, z = 34.7 },
-- Add more Ammu-Nation store locations as needed
}
-- Define NPC vendors
Config.NPCVendors = {
{
name = "Gun Dealer",
model = "s_m_m_gunsmith_01",
location = { x = 2126.7, y = 4794.1, z = 41.1 },
heading = 90.0,
items = { 1, 2, 3 } -- Item IDs from Config.Items
},
{
name = "Weapon Trader",
model = "s_m_m_gunsmith_02",
location = { x = 256.5, y = -50.0, z = 69.2 },
heading = 45.0,
items = { 2, 3 } -- Item IDs from Config.Items
},
-- Add more vendors as needed
}
-- Define spawn points for each role
Config.SpawnPoints = {
cop = vector3(452.6, -980.0, 30.7), -- Police station location
robber = vector3(2126.7, 4794.1, 41.1) -- Countryside airport location
}
-- Define prison location
Config.PrisonLocation = vector3(1651.0, 2570.0, 45.5) -- Prison coordinates
-- =====================================
-- VARIABLES
-- =====================================
-- Player-related variables
local role = nil
local playerCash = 0
local playerStats = { heists = 0, arrests = 0, rewards = 0, experience = 0, level = 1 }
local currentObjective = nil
local wantedLevel = 0
local playerWeapons = {}
local playerAmmo = {}
-- =====================================
-- HELPER FUNCTIONS
-- =====================================
-- Function to display notifications on screen
function ShowNotification(text)
SetNotificationTextEntry("STRING")
AddTextComponentSubstringPlayerName(text)
DrawNotification(false, true)
end
-- Function to display help text
function DisplayHelpText(text)
BeginTextCommandDisplayHelp("STRING")
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayHelp(0, false, true, -1)
end
-- Function to spawn player based on role
local function spawnPlayer(role)
local spawnPoint = Config.SpawnPoints[role]
if spawnPoint then
SetEntityCoords(PlayerPedId(), spawnPoint.x, spawnPoint.y, spawnPoint.z, false, false, false, true)
-- Additional spawn logic can be added here (e.g., setting heading, animations)
else
print("Error: Invalid role for spawning: " .. tostring(role))
end
end
-- =====================================
-- NETWORK EVENTS
-- =====================================
-- Request player data when the player spawns
AddEventHandler('playerSpawned', function()
-- Request player data
TriggerServerEvent('cops_and_robbers:requestPlayerData')
-- Show NUI role selection
SetNuiFocus(true, true)
SendNUIMessage({ action = 'showRoleSelection' })
end)
-- Receive player data from the server
RegisterNetEvent('cops_and_robbers:receivePlayerData')
AddEventHandler('cops_and_robbers:receivePlayerData', function(data)
if not data then
print("Error: Received nil data in 'receivePlayerData'")
return
end
playerCash = data.cash or 0
playerStats = data.stats or { heists = 0, arrests = 0, rewards = 0, experience = 0, level = 1 }
role = data.role
spawnPlayer(role) -- Spawn player based on received role
local playerPed = PlayerPedId()
if data.weapons then
for weaponName, ammo in pairs(data.weapons) do
local weaponHash = GetHashKey(weaponName)
GiveWeaponToPed(playerPed, weaponHash, ammo or 0, false, false)
playerWeapons[weaponName] = true
playerAmmo[weaponName] = ammo or 0
end
end
-- Restore inventory items and money display if applicable
end)
-- Receive item list from the server (Registered once)
RegisterNetEvent('cops_and_robbers:sendItemList')
AddEventHandler('cops_and_robbers:sendItemList', function(storeName, itemList)
SetNuiFocus(true, true)
SendNUIMessage({
action = 'openStore',
storeName = storeName,
items = itemList
})
end)
-- Notify cops of a bank robbery with GPS update and sound
RegisterNetEvent('cops_and_robbers:notifyBankRobbery')
AddEventHandler('cops_and_robbers:notifyBankRobbery', function(bankId, bankLocation, bankName)
if role == 'cop' then
ShowNotification("~r~Bank Robbery in Progress!~s~\nBank: " .. bankName)
SetNewWaypoint(bankLocation.x, bankLocation.y)
-- Optionally, play a sound or other alert here
end
end)
-- Jail System: Send player to jail
RegisterNetEvent('cops_and_robbers:sendToJail')
AddEventHandler('cops_and_robbers:sendToJail', function(jailTime)
SetEntityCoords(PlayerPedId(), Config.PrisonLocation.x, Config.PrisonLocation.y, Config.PrisonLocation.z, false, false, false, true)
ShowNotification("~r~You have been sent to jail for " .. jailTime .. " seconds.")
-- Implement jail time countdown and restrictions
Citizen.CreateThread(function()
local remainingTime = jailTime
while remainingTime > 0 do
Citizen.Wait(1000) -- Wait 1 second
remainingTime = remainingTime - 1
-- Restrict player actions while in jail
DisableControlAction(0, 24, true) -- Disable attack
DisableControlAction(0, 25, true) -- Disable aim
DisableControlAction(0, 47, true) -- Disable weapon (G key)
DisableControlAction(0, 58, true) -- Disable weapon (last argument, weapon wheel)
-- Add more restrictions as needed
end
-- Release player from jail
spawnPlayer(role)
ShowNotification("~g~You have been released from jail.")
end)
end)
-- Purchase Confirmation
RegisterNetEvent('cops_and_robbers:purchaseConfirmed')
AddEventHandler('cops_and_robbers:purchaseConfirmed', function(itemId, quantity)
quantity = quantity or 1
-- Find item details
local itemName = nil
for _, item in ipairs(Config.Items) do
if item.itemId == itemId then
itemName = item.name
break
end
end
if itemName then
ShowNotification('You purchased: ' .. quantity .. ' x ' .. itemName)
else
ShowNotification('Purchase successful.')
end
end)
-- Purchase Failure
RegisterNetEvent('cops_and_robbers:purchaseFailed')
AddEventHandler('cops_and_robbers:purchaseFailed', function(reason)
ShowNotification('Purchase failed: ' .. reason)
end)
-- Sell Confirmation
RegisterNetEvent('cops_and_robbers:sellConfirmed')
AddEventHandler('cops_and_robbers:sellConfirmed', function(itemId, quantity)
quantity = quantity or 1
-- Find item details
local itemName = nil
for _, item in ipairs(Config.Items) do
if item.itemId == itemId then
itemName = item.name
break
end
end
if itemName then
ShowNotification('You sold: ' .. quantity .. ' x ' .. itemName)
else
ShowNotification('Sale successful.')
end
end)
-- Sell Failure
RegisterNetEvent('cops_and_robbers:sellFailed')
AddEventHandler('cops_and_robbers:sellFailed', function(reason)
ShowNotification('Sale failed: ' .. reason)
end)
-- Add Weapon
RegisterNetEvent('cops_and_robbers:addWeapon')
AddEventHandler('cops_and_robbers:addWeapon', function(weaponName)
local playerPed = PlayerPedId()
local weaponHash = GetHashKey(weaponName)
GiveWeaponToPed(playerPed, weaponHash, 0, false, false)
playerWeapons[weaponName] = true
ShowNotification("Weapon added: " .. weaponName)
end)
-- Remove Weapon
RegisterNetEvent('cops_and_robbers:removeWeapon')
AddEventHandler('cops_and_robbers:removeWeapon', function(weaponName)
local playerPed = PlayerPedId()
local weaponHash = GetHashKey(weaponName)
RemoveWeaponFromPed(playerPed, weaponHash)
playerWeapons[weaponName] = nil
ShowNotification("Weapon removed: " .. weaponName)
end)
-- Add Ammo
RegisterNetEvent('cops_and_robbers:addAmmo')
AddEventHandler('cops_and_robbers:addAmmo', function(weaponName, ammoCount)
local playerPed = PlayerPedId()
local weaponHash = GetHashKey(weaponName)
if HasPedGotWeapon(playerPed, weaponHash, false) then
AddAmmoToPed(playerPed, weaponHash, ammoCount)
playerAmmo[weaponName] = (playerAmmo[weaponName] or 0) + ammoCount
ShowNotification("Added " .. ammoCount .. " ammo to " .. weaponName)
else
ShowNotification("You don't have the weapon for this ammo.")
end
end)
-- Apply Armor
RegisterNetEvent('cops_and_robbers:applyArmor')
AddEventHandler('cops_and_robbers:applyArmor', function(armorType)
local playerPed = PlayerPedId()
if armorType == "armor" then
SetPedArmour(playerPed, 50)
ShowNotification("~g~Armor applied: Light Armor (~w~50 Armor)")
elseif armorType == "heavy_armor" then
SetPedArmour(playerPed, 100)
ShowNotification("~g~Armor applied: Heavy Armor (~w~100 Armor)")
else
ShowNotification("Invalid armor type.")
end
end)
-- =====================================
-- NUI CALLBACKS
-- =====================================
-- NUI Callback for role selection
RegisterNUICallback('selectRole', function(data, cb)
local selectedRole = data.role
if selectedRole == 'cop' or selectedRole == 'robber' then
TriggerServerEvent('cops_and_robbers:setPlayerRole', selectedRole)
SetNuiFocus(false, false)
cb('ok')
else
ShowNotification("Invalid role selected.")
cb('error')
end
end)
-- =====================================
-- STORE FUNCTIONS
-- =====================================
-- Function to open the store
function openStore(storeName, storeType, vendorItems)
TriggerServerEvent('cops_and_robbers:getItemList', storeType, vendorItems, storeName)
end
-- =====================================
-- STORE AND VENDOR INTERACTION
-- =====================================
-- Detect player near a store or vendor
Citizen.CreateThread(function()
while true do
Citizen.Wait(500) -- Check every 0.5 seconds to optimize performance
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
-- Check proximity to Ammu-Nation stores
for _, store in ipairs(Config.AmmuNationStores) do
local distance = #(playerCoords - vector3(store.x, store.y, store.z))
if distance < 2.0 then
DisplayHelpText('Press ~INPUT_CONTEXT~ to open Ammu-Nation')
if IsControlJustPressed(0, 51) then -- E key
openStore('Ammu-Nation', 'AmmuNation', nil)
end
end
end
-- Check proximity to NPC vendors
for _, vendor in ipairs(Config.NPCVendors) do
local vendorCoords = vector3(vendor.location.x, vendor.location.y, vendor.location.z)
local distance = #(playerCoords - vendorCoords)
if distance < 2.0 then
DisplayHelpText('Press ~INPUT_CONTEXT~ to talk to ' .. vendor.name)
if IsControlJustPressed(0, 51) then
openStore(vendor.name, 'Vendor', vendor.items)
end
end
end
end
end)
-- =====================================
-- NPC VENDOR SPAWN
-- =====================================
-- Spawn NPC vendors in the world
Citizen.CreateThread(function()
for _, vendor in ipairs(Config.NPCVendors) do
local hash = GetHashKey(vendor.model)
RequestModel(hash)
while not HasModelLoaded(hash) do
Citizen.Wait(100)
end
local npc = CreatePed(4, hash, vendor.location.x, vendor.location.y, vendor.location.z - 1.0, vendor.heading, false, true)
SetEntityInvincible(npc, true)
SetBlockingOfNonTemporaryEvents(npc, true)
FreezeEntityPosition(npc, true)
-- Optionally, add more NPC configurations here
end
end)
-- =====================================
-- PLAYER POSITION & WANTED LEVEL UPDATES
-- =====================================
-- Periodically send player position and wanted level to the server
Citizen.CreateThread(function()
while true do
Citizen.Wait(5000) -- Every 5 seconds
local playerPed = PlayerPedId()
if DoesEntityExist(playerPed) then
local playerPos = GetEntityCoords(playerPed)
TriggerServerEvent('cops_and_robbers:updatePosition', playerPos, wantedLevel)
end
end
end)