Cannot retrieve points of a user in all pages

I use the following code to appoint a score to the user. ( wordpress )

}
function set_best_answer_and_report(){
global $wpdb,$userdata,$answers;
	if($_GET["report"]){
		$report=$_GET["report"];
		if($userdata->ID){
		$p=$_GET["p"];
		$wpdb->query("UPDATE ".$wpdb->comments." SET reported=1 WHERE comment_ID=$report");
		}
	}
	if($_GET["ba"])
	{
		$ba=$_GET["ba"];
		$comment= get_comment($ba);
		$p=$comment->comment_post_ID;
		$data=get_postdata($p);
		
		
		$commentdata=get_comment($ba);
		if($data["Author_ID"]==$userdata->ID and $data["comment_status"]!="closed")
		{
		$user=get_userdata($commentdata->user_id);
		$wpdb->query("UPDATE ".$wpdb->comments." SET ba=1 WHERE comment_ID=$ba");
		$wpdb->query("UPDATE ".$wpdb->posts." SET comment_status='closed' WHERE ID=$p");
		$qya_options=get_option("qya_options");
		if(isset($user->qya_points)){
		 	$points=$user->qya_points;
		 }else{
			$points=0;
		}
		update_usermeta($commentdata->user_id,"qya_points",$points+$qya_options["qya_extrappa"]);
		
		}
		
	}
}

I input this code in loggedin-in.php in order to echo the score of the user always on the sidebar:

<?php the_points();?>

However, when I move to the another page, User’s score either becomes 0 or the different score from a different user, mainly who has the least score. How can I fix this issue?

Where is your function “the_points” coming from?

Do you mean this:



}
	function the_points(){
		global $authordata,$profiledata;
		if($authordata->qya_points!=""){
			echo $authordata->qya_points;
		}else{if($profiledata->qya_points!=""){
			echo $profiledata->qya_points;
			}else
			{
			echo 0;
			}
				
		}
	}