Question about creating peds

Citizen.CreateThread(function()
    local networkId = nil
	local hash = GetHashKey("a_f_y_carclub_01")
        
    RegisterCommand("bodyguard", function()
    while not HasModelLoaded(hash) do
    RequestModel(hash)
    Wait(20)
    end
	
	local playerPed = PlayerPedId()
    local player = GetPlayerPed(playerPed)
    local playerPosition = GetOffsetFromEntityInWorldCoords(playerPed, 0.0, 5.0, 0.0)
    local playerGroup = GetPedGroupIndex(playerPed)
    ped = CreatePed("PED_TYPE_SPECIAL", "a_f_y_carclub_01", playerPosition.x, playerPosition.y, playerPosition.z, 1, true, true)
    SetPedCanSwitchWeapon(ped, false)
    SetPedAsGroupMember(ped, playerGroup)
    GiveWeaponToPed(ped, "weapon_pistol" , 999, true, true)
	SetPedComponentVariation(ped, 0, 2, 2, 0)
	SetPedComponentVariation(ped, 2, 1, 0, 0)
	SetPedComponentVariation(ped, 3, 3, 0, 0)
	SetPedComponentVariation(ped, 4, 0, 4, 0)
	SetPedComponentVariation(ped, 6, 1, 0, 0)
	SetPedComponentVariation(ped, 5, 3, 3, 0)
	SetPedPropIndex(ped, 0, 1, 0, 0)
	SetPedPropIndex(ped, 1, 0, 0, 0)
	networkId = NetworkGetNetworkIdFromEntity(ped)
	local ped = NetworkGetEntityFromNetworkId(networkId)
    end)
end)

So im trying to make bodyguard ped but im new to this so how could i make it impossible to spawn more than 1 ped?

if you’re asking on how to ensure that a player only ever has one bodyguard active, add a global variable to keep track of if the player has spawned a bodyguard.

I dont know if this code works, i just wrote it here quickly but should push you in the right direction.

local pedHandle = nil
local pedModel = "a_f_y_carclub_01"
local pedHash = GetHashKey(pedModel)
	
RegisterCommand("bodyguard", function()
	-- check if we have a bodyguard already out
	if pedHandle ~= nil then return end

	while not HasModelLoaded(pedHash) do
		RequestModel(pedHash)
		Citizen.Wait(20)
	end

	local playerPed = PlayerPedId()
	local playerPosition = GetOffsetFromEntityInWorldCoords(playerPed, 0.0, 5.0, 0.0)
	local playerGroup = GetPedGroupIndex(playerPed)
	
	pedHandle = CreatePed("PED_TYPE_SPECIAL", pedModel, playerPosition.x, playerPosition.y, playerPosition.z, 1, true, true)
	SetPedCanSwitchWeapon(pedHandle, false)
	SetPedAsGroupMember(pedHandle, playerGroup)
	
	GiveWeaponToPed(pedHandle, "weapon_pistol" , 999, true, true)
	SetPedComponentVariation(pedHandle, 0, 2, 2, 0)
	SetPedComponentVariation(pedHandle, 2, 1, 0, 0)
	SetPedComponentVariation(pedHandle, 3, 3, 0, 0)
	SetPedComponentVariation(pedHandle, 4, 0, 4, 0)
	SetPedComponentVariation(pedHandle, 6, 1, 0, 0)
	SetPedComponentVariation(pedHandle, 5, 3, 3, 0)
	SetPedPropIndex(pedHandle, 0, 1, 0, 0)
	SetPedPropIndex(pedHandle, 1, 0, 0, 0)
	
	-- start the listener
	startBodyGuardListener()
end)
-- create a function to watch over the bodyguard until his death
startBodyGuardListener = function()
	Citizen.CreateThread(function()
		while pedHandle ~= nil and DoesEntityExist(pedHandle) and GetEntityHealth(pedHandle) > 0 do
			Citizen.Wait(3)
		end
		pedHandle = nil
	end)
end

Thanks