You have to use pev_valid(ent).
You have to do something like that:
Code:
public forward_keyvalue(ent2, kvdid) {
if (!pev_valid(ent2)) // if entity isn't valid
return FMRES_IGNORED // do not perform any action
// do something here
return FMRES_IGNORED // at the end of function
}