FindFirstObject problem

Well as the title say i have a problem with these functions sometimes i just get 0 as Object ID and this is only fixing after closing the game and restarting the server…

I dont know if i’m doing something wrong or what this is my code:

function getNearestObject()
	local toReturn = 0
	local localPlayer = GetPlayerPed(-1)
	local x, y, z = table.unpack(GetEntityCoords(localPlayer, true))
	local handle, object = FindFirstObject()
	local finished = false
	repeat
		local x2, y2, z2 = table.unpack(GetEntityCoords(object, true))
		local objHash = GetEntityModel(object)
		for i = 1, #objects do
			local obj = objects[i]
			if objHash == GetHashKey(obj.hash) and Vdist(x, y, z, x2, y2, z2) < 5.0 then
				toReturn = object
				break
			end
		end
		finished, object = FindNextObject(handle)
	until not finished
	EndFindObject(handle)
	return toReturn
end