Skip to content

Commit 385b225

Browse files
authored
Check on server side if the player has radio
1 parent 903a9ee commit 385b225

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

client.lua

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,10 @@ local function IsRadioOn()
9898
return onRadio
9999
end
100100

101-
local function DoRadioCheck(PlayerItems)
102-
local _hasRadio = false
103-
104-
for _, item in pairs(PlayerItems) do
105-
if item.name == "radio" then
106-
_hasRadio = true
107-
break;
108-
end
109-
end
110-
111-
hasRadio = _hasRadio
101+
local function DoRadioCheck()
102+
QBCore.Functions.TriggerCallback("qb-radio::server::hasRadio", function(value)
103+
hasRadio = value
104+
end)
112105
end
113106

114107
--Exports
@@ -119,27 +112,26 @@ exports("IsRadioOn", IsRadioOn)
119112
-- Handles state right when the player selects their character and location.
120113
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
121114
PlayerData = QBCore.Functions.GetPlayerData()
122-
DoRadioCheck(PlayerData.items)
115+
DoRadioCheck()
123116
end)
124117

125118
-- Resets state on logout, in case of character change.
126119
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
127-
DoRadioCheck({})
128120
PlayerData = {}
129121
leaveradio()
130122
end)
131123

132124
-- Handles state when PlayerData is changed. We're just looking for inventory updates.
133125
RegisterNetEvent('QBCore:Player:SetPlayerData', function(val)
134126
PlayerData = val
135-
DoRadioCheck(PlayerData.items)
127+
DoRadioCheck()
136128
end)
137129

138130
-- Handles state if resource is restarted live.
139131
AddEventHandler('onResourceStart', function(resource)
140132
if GetCurrentResourceName() == resource then
141133
PlayerData = QBCore.Functions.GetPlayerData()
142-
DoRadioCheck(PlayerData.items)
134+
DoRadioCheck()
143135
end
144136
end)
145137

server.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ for channel, config in pairs(Config.RestrictedChannels) do
1010
return config[Player.PlayerData.job.name] and Player.PlayerData.job.onduty
1111
end)
1212
end
13+
14+
QBCore.Functions.CreateCallback("qb-radio::server::hasRadio", function(source, cb)
15+
local hasItems = exports['qb-inventory']:HasItem(source, "radio", 1)
16+
return hasItems
17+
end)

0 commit comments

Comments
 (0)