I made some changes, but this code is not tested but should just work the same way as before.

Question: bestill is a defined var somewhere? Better solution would be to use a flag or a skill to do this job.
Code:

var snakemorph = 0;

Entity* snakebod;

function headhit // only do this upon impact!{my.invisible=on;}
{
my.invisible=on;
ent_morph (snakebod, "snakedead.mdl");
bestill=1;
}

action snakehead
{
// 1 is up, -1 is down
my.health=20;
my.gegner=on;
var direction = 1;
var speed = 1;
my.enable_entity=on;
my.enable_impact=on;
MY.EVENT=headhit;

while (!bestill)
{
walk_percent=(walk_percent+5*time)%100;
ent_animate(me,"walk",walk_percent,ANM_CYCLE);

my.x -= direction * speed;

vec_set(temp,my.x);
temp.z+=direction * speed;
trace_mode = IGNORE_ME;

if (trace(my.x, temp) != 0)
{
direction *= -1;
}
wait(1);
}
my.enable_entity=off;
my.enable_impact=off;
MY.EVENT=null;
}

action snakebody
{
snakebod=me; // assign entity snakebod to this models action

// 1 is up, -1 is down
my.health=20;
my.gegner=on;
var direction = 1;
var speed = 1;

my.enable_impact=on;
my.event=lev_2_event;

while(!bestill)
{
walk_percent=(walk_percent+5*time)%100;
ent_animate(me,"walk",walk_percent,ANM_CYCLE);

// move ball in the right direction
my.x -= direction * speed;

// change direction if obstacle is in the way

vec_set(temp,my.x);
temp.z+=direction * speed;
trace_mode = IGNORE_ME;

if (trace(my.x, temp) != 0)
{
direction *= -1;
}
wait(1);
}

my.enable_impact = off;
my.event = null; //MAKE THE SNAKE UNSENSIBLE FOR IMPACTS
}