-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.lua
174 lines (143 loc) · 4.03 KB
/
main.lua
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
ESX = nil
local Weapons = {}
local Loaded = false
Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(0)
end
while not Loaded do
Citizen.Wait(500)
end
while true do
Citizen.Wait(500)
local playerPed = PlayerPedId()
for i=1, #Config.RealWeapons, 1 do
local weaponHash = GetHashKey(Config.RealWeapons[i].name)
if HasPedGotWeapon(playerPed, weaponHash, false) then
local onPlayer = false
for weaponName, entity in pairs(Weapons) do
if weaponName == Config.RealWeapons[i].name then
onPlayer = true
break
end
end
if not onPlayer and weaponHash ~= GetSelectedPedWeapon(playerPed) then
SetGear(Config.RealWeapons[i].name)
elseif onPlayer and weaponHash == GetSelectedPedWeapon(playerPed) then
RemoveGear(Config.RealWeapons[i].name)
end
end
end
end
end)
AddEventHandler('skinchanger:modelLoaded', function()
SetGears()
Loaded = true
end)
RegisterNetEvent('esx:removeWeapon')
AddEventHandler('esx:removeWeapon', function(weaponName)
RemoveGear(weaponName)
end)
-- Remove only one weapon that's on the ped
function RemoveGear(weapon)
local _Weapons = {}
for weaponName, entity in pairs(Weapons) do
if weaponName ~= weapon then
_Weapons[weaponName] = entity
else
ESX.Game.DeleteObject(entity)
end
end
Weapons = _Weapons
end
-- Remove all weapons that are on the ped
function RemoveGears()
for weaponName, entity in pairs(Weapons) do
ESX.Game.DeleteObject(entity)
end
Weapons = {}
end
-- Add one weapon on the ped
function SetGear(weapon)
local bone = nil
local boneX = 0.0
local boneY = 0.0
local boneZ = 0.0
local boneXRot = 0.0
local boneYRot = 0.0
local boneZRot = 0.0
local playerPed = PlayerPedId()
local model = nil
local playerData = ESX.GetPlayerData()
for i=1, #Config.RealWeapons, 1 do
if Config.RealWeapons[i].name == weapon then
bone = Config.RealWeapons[i].bone
boneX = Config.RealWeapons[i].x
boneY = Config.RealWeapons[i].y
boneZ = Config.RealWeapons[i].z
boneXRot = Config.RealWeapons[i].xRot
boneYRot = Config.RealWeapons[i].yRot
boneZRot = Config.RealWeapons[i].zRot
model = Config.RealWeapons[i].model
break
end
end
ESX.Game.SpawnObject(model, {
x = x,
y = y,
z = z
}, function(object)
local boneIndex = GetPedBoneIndex(playerPed, bone)
local bonePos = GetWorldPositionOfEntityBone(playerPed, boneIndex)
AttachEntityToEntity(object, playerPed, boneIndex, boneX, boneY, boneZ, boneXRot, boneYRot, boneZRot, false, false, false, false, 2, true)
Weapons[weapon] = object
end)
end
-- Add all the weapons in the xPlayer's loadout
-- on the ped
function SetGears()
local bone = nil
local boneX = 0.0
local boneY = 0.0
local boneZ = 0.0
local boneXRot = 0.0
local boneYRot = 0.0
local boneZRot = 0.0
local playerPed = PlayerPedId()
local model = nil
local playerData = ESX.GetPlayerData()
local weapon = nil
for i=1, #playerData.loadout, 1 do
for j=1, #Config.RealWeapons, 1 do
if Config.RealWeapons[j].name == playerData.loadout[i].name then
bone = Config.RealWeapons[j].bone
boneX = Config.RealWeapons[j].x
boneY = Config.RealWeapons[j].y
boneZ = Config.RealWeapons[j].z
boneXRot = Config.RealWeapons[j].xRot
boneYRot = Config.RealWeapons[j].yRot
boneZRot = Config.RealWeapons[j].zRot
model = Config.RealWeapons[j].model
weapon = Config.RealWeapons[j].name
break
end
end
local _wait = true
ESX.Game.SpawnObject(model, {
x = x,
y = y,
z = z
}, function(object)
local boneIndex = GetPedBoneIndex(playerPed, bone)
local bonePos = GetWorldPositionOfEntityBone(playerPed, boneIndex)
AttachEntityToEntity(object, playerPed, boneIndex, boneX, boneY, boneZ, boneXRot, boneYRot, boneZRot, false, false, false, false, 2, true)
Weapons[weapon] = object
_wait = false
end)
-- wait for async task
while _wait do
Citizen.Wait(10)
end
end
end