Code:
moveplayer(team)
{
    self.pers["team"] = team;
    self.sessionteam = self.pers["team"];
    self.pers["weapon"] = undefined;
    self.pers["savedmodel"] = undefined;
    self setClientCvar("ui_allow_weaponchange", "1");
    if (self.sessionstate!="dead") 
	{
		self.sessionstate = team;
		self suicide();     
	}
    self notify("joined_team");
    self notify("end_respawn");
    maps\mp\gametypes\dm::spawnSpectator();
}
Ok i managed to do it modifying a code i found on this forum. It does the job but it doesn't work 100% correctly. When the player is moved to spectator and then tries to rejoin the options "autoassing" and the previous team are grayed out. So i guess it is not moved properly to spectator. Any idea how to fix this?