Help with hunting script!

So im currently working on a script that makes it possible to kill deers and then slaughter them. i have a problem though and need some help by someone a little more experienced. the script is done just need to get the locals working.

function Hunting()

	LoadModel('a_c_deer')
	LoadAnimDict('amb@medic@standing@kneel@base')
	LoadAnimDict('anim@gangops@facility@servers@bodysearch@')

	Citizen.CreateThread(function()
		while true do
			local sleep = 500
			
			local plyCoords = GetEntityCoords(PlayerPedId())
			local Animal = GetHashKey('a_c_deer') 
			local ClosestAnimal = GetClosestPed(Animal)
			local AnimalCoords = GetEntityCoords(ClosestAnimal)

			local distance = GetDistanceBetweenCoords(plyCoords, AnimalCoords, true)
			
			local AnimalHealth = GetEntityHealth(ClosestAnimal)
			
			if AnimalHealth <= 0 then			
				local AnimalBlip = AddBlipForEntity(ClosestAnimal)
				SetBlipSprite(AnimalBlip, 153)
				SetBlipColour(AnimalBlip, 1)
				BeginTextCommandSetBlipName("STRING")
				AddTextComponentString('Deer - Animal')
				EndTextCommandSetBlipName(AnimalBlip)

				if distance < 2.0 then
					ESX.Game.Utils.DrawText3D({x = AnimalCoords.x, y = AnimalCoords.y, z = AnimalCoords.z + 1}, '[E] Slaughter Animal', 0.4)
					if IsControlJustReleased(0, Keys['E']) then
						if GetSelectedPedWeapon(PlayerPedId()) == GetHashKey('WEAPON_KNIFE')  then
							if DoesEntityExist(ClosestAnimal) then
								SlaughterAnimal()
							end
						else
							ESX.ShowNotification('You need to use the knife!')
						end
					end				
				end
				Citizen.Wait(sleep)
		    end
	    end
	end)
end

Thats the code i need help with. all hel is appreciated! thanks!

GetClosestPed wont cut it. I believe FindFirstPed, FindNextPed, EndFindPed will, though. That is, if deer are peds, which I’m guessing they are.

Other than that, what other help do you need?

Lua example of what I mentioned:

local handle, ped = FindFirstPed()
local success

repeat

success, ped = FindNextPed(handle)
until not success

EndFindPed(handle)

I’ll try, brb! and thanks! Didnt work still need help.

hmm im gonna test a bit

1 Like

Thanks! If you need the rest of the script ill gladly send it too you. i really want this so you can go and hunt all the deers around the map:)

Any progress?

I’m not gonna be on the pc today sorry it is Christmas

1 Like

Haha fully understand, same here haha. Merry Christmas man!

You’re going to download this script here, I’m from Brazil here

Hi is this for vRP? I try the french ESX version and works on the vRP for the hunting but once i gather the meat doesnt show it on my inventory so i can sell it to the butcher… Any help with that? At least poit me in a direction please.

@Loffe Hello i am making same hunting script that you can hunt deers around the map, did u find the solution how to get closest deer, cause i havent :smiley: if you would like to help me, that will be very much appreciated!

-kind regards rönni

Huh, this does work, however it makes no sense (to me) that you must use repeat and not other ways of looping. Is there any proper documentation about this?
Here’s how I did it

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1)
		local ped = PlayerPedId()
		local random,npc = FindFirstPed()
		repeat
		if IsPedDeadOrDying(npc) and not IsPedAPlayer(npc) and not IsPedInAnyVehicle(ped) and not IsPedInAnyVehicle(npc) then
			if GetEntityModel(npc) == 1581098148 then
				local distance = GetDistanceBetweenCoords(GetEntityCoords(ped),GetEntityCoords(npc),true)
				if distance <= 2.0 then
				print('Press E to skin')
					if IsControlJustPressed(0,38) then
						local gunInHand = GetSelectedPedWeapon(ped)
						if gunInHand == GetHashKey("WEAPON_KNIFE") or gunInHand == GetHashKey("WEAPON_DAGGER") or gunInHand == GetHashKey("WEAPON_MACHETE") or gunInHand == GetHashKey("WEAPON_SWITCHBLADE") or gunInHand == GetHashKey("WEAPON_HATCHET") or gunInHand == GetHashKey("WEAPON_BATTLEAXE") or gunInHand == GetHashKey("WEAPON_STONE_HATCHET") then
							print('you filthy monster')
							SetEntityCoords(ped, GetEntityCoords(npc),0,0,0,1)
							success = true
						end
					end
				end
			end
		end
			success,npc = FindNextPed(random)
		until not success
		EndFindPed(random)
	end
end)

@Wallon So it should work only with that code, and i can ad more animals in there right?

-kind regards rönni

You need to loop through all the closest peds that for performace sake is dead. and If the ped is a dear you can add a blip or whatever. Then If your distance to the animal is lets say > 400 then you should remove the animal so you dont keep unused stuff laying around