Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/g drugselling #1

Merged
merged 17 commits into from
Feb 19, 2023
Prev Previous commit
Next Next commit
Implement NPC spawning/despawning
  • Loading branch information
Giana committed Feb 19, 2023
commit 4b8d877677b68ca2ac08ccf4f41a74936b31771a
100 changes: 100 additions & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,75 @@
local QBCore = exports['qb-core']:GetCoreObject()

local IsSelling = false
local Peds = {}

-- Functions --

function createPed(hash, ...)
RequestModel(hash)
while not HasModelLoaded(hash) do
Citizen.Wait(100)
end
local ped = CreatePed(26, hash, ...)
SetModelAsNoLongerNeeded(hash)
return ped
end

function createClientNpc(index)
if (Peds[index]) then
deleteClientNpc(index)
end
Peds[index] = {}
local location = Config.SellLocations[index]
local ped = createPed(location.npc.ped, location.coords.x, location.coords.y, location.coords.z - 0.97, location.npc.heading, false, true)
FreezeEntityPosition(ped, true)
SetEntityHeading(ped, location.npc.heading)
SetEntityInvincible(ped, true)
SetBlockingOfNonTemporaryEvents(ped, true)
Peds[index].npc = ped
end

function deleteClientNpc(index)
if (Peds[index]) then
DeleteEntity(Peds[index].npc)
Peds[index] = nil
end
end

function attemptSell(sellLocationIndex)
local notificationsEnabled = Config.SellLocations[sellLocationIndex].notificationsEnabled
if IsSelling then
return
end
IsSelling = true
QBCore.Functions.TriggerCallback('g-drugselling:server:getCopCount', function(copCount)
if copCount >= Config.SellLocations[sellLocationIndex].policeRequired then
QBCore.Functions.TriggerCallback('g-drugselling:server:getSellableItems', function(sellableItems)
if sellableItems and #sellableItems > 0 then
if notificationsEnabled then
QBCore.Functions.Notify(Lang:t('info.selling'))
end
TriggerServerEvent('g-drugselling:server:sellItems', sellLocationIndex, sellableItems, Config.SellLocations[sellLocationIndex].money_reward_type)
Citizen.Wait(10000)
IsSelling = false
else
if notificationsEnabled then
QBCore.Functions.Notify(Lang:t('error.not_enough'))
end
Citizen.Wait(10000)
IsSelling = false
end
end, sellLocationIndex)
else
if notificationsEnabled then
QBCore.Functions.Notify(Lang:t('error.cannot_sell'))
end
Citizen.Wait(20000)
IsSelling = false
end
end)
end

-- Events --

RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
Expand Down Expand Up @@ -40,6 +112,34 @@ Citizen.CreateThread(function()
end
end)

-- NPC ped setup
Citizen.CreateThread(function()
while true do
if LocalPlayer.state.isLoggedIn then
local ped = PlayerPedId()
local pos = GetEntityCoords(ped)
local inRange = false
for k, v in pairs(Config.SellLocations) do
if v.active and v.npc.enabled then
local dist = #(pos - v.coords)
if dist < Config.NpcRenderDistance then
if not Peds[k] then
createClientNpc(k)
end
inRange = true
elseif Peds[k] then
deleteClientNpc(k)
end
end
end
if not inRange then
Citizen.Wait(2000)
end
end
Citizen.Wait(3)
end
end)

-- Walk up & drive up
Citizen.CreateThread(function()
while true do
Expand Down