So I am a little stuck, I was able to get the bodyguard script and make it spawn server side. But the bodyguards act different in each client. For me they were shooting my friend, while for my friend they were shooting me. I am unaware why, but I think it has something to do with GetPlayerPed returning 0.
Following is the code:
Client
function UnloadBodyguard()
for k, guard in pairs(Bodyguard.Guards) do
if(guard ~= nil) then
DeletePed(guard)
Bodyguard.Guards[k] = nil
end
end
end
RegisterNetEvent("spawnped")
AddEventHandler("spawnped", function()
local BodyGuardSkinID = GetHashKey(Bodyguard.GuardSkin)
local playerPed = PlayerPedId()
local player = GetPlayerPed(playerPed)
local playerPosition = GetOffsetFromEntityInWorldCoords(playerPed, 0.0, 5.0, 0.0)
local playerGroup = GetPedGroupIndex(playerPed)
if Bodyguard.SpawnMultiple == false then
UnloadBodyguard()
end
Citizen.Wait(10)
RequestModel(BodyGuardSkinID)
while(not HasModelLoaded(BodyGuardSkinID)) do
Citizen.Wait(10)
end
for i = 0, Bodyguard.GuardAmount, 1 do
Bodyguard.Guards[i] = CreatePed(26, BodyGuardSkinID, playerPosition.x, playerPosition.y, playerPosition.z, 1, false, true)
SetPedCanSwitchWeapon(Bodyguard.Guards[i],false)
SetPedAsGroupMember(Bodyguard.Guards[i], playerGroup)
if Bodyguard.SetInvincible == true then
SetEntityInvincible(Bodyguard.Guards[i], true)
else
SetEntityInvincible(Bodyguard.Guards[i], false)
end
if Bodyguard.GiveWeapon == true then
GiveWeaponToPed(Bodyguard.Guards[i], GetHashKey(Bodyguard.GuardWeapon), 100, true, true)
end
end
SetModelAsNoLongerNeeded(BodyGuardSkinID)
end)
Config
Bodyguard = {}
Bodyguard.Guards = {}
Bodyguard.GuardAmount = 3 --0 = 1, 1 = 2, 2 = 3 And So On.
Bodyguard.SpawnMultiple = false --False = Removes previous bodyguard(s), True = Spawns multiple bodyguards without removing.
Bodyguard.GuardSkin = 's_m_m_highsec_01' --Bodyguard ped.
Bodyguard.GiveWeapon = true --Give weapon to bodyguard.
Bodyguard.GuardWeapon = 'WEAPON_APPISTOL' --Weapon for bodyguard.
Bodyguard.SetInvincible = false --Sets bodyguard invincible.
Server
RegisterCommand("spawnped", function(source)
TriggerClientEvent("spawnped", -1)
end)
Also, I would like to improve it by adding up a few features, if anyone can point me directions.
Whenever they are not in a car, they will have a weapon_assaultrifle instead.
I tried enabling them to switch weapons, giving them the weapon_assaultirifle and forcing them to use pistol in car, and if not in car use assaultrifle, but it didn’t seem to work
Also, I would love to add a function where if I am in passanger seat, I can choose a waypoint in the map and they will drive me there, similar to vMenu auto-drive. And lastly, if they can’t fit in my car, they will get the closest car and follow me