HUD not updating?

I have a scoreboard like thing built into my HUD, but whenever a team scores the HUD does not update.
Here is the code in the shared.lua :


CopScore = 1
RobScore = 1
 
timer.Create("FlagTimer", .1, 0, function()
	for a,b in ipairs(ents.FindByClass("weapon_flag")) do
		if !IsValid(b) then continue end
		for k,v in ipairs(ents.FindByClass("info_player_red")) do
			if !IsValid(v) then continue end
			local pos1 = v:GetPos()
			local pos2 = b:GetPos()
			if( pos1:Distance( pos2 ) < 50) then
			RobScore = RobScore + 1 
				for k,v in pairs(player.GetAll()) do
				v:Kill()
				end
			end
		end
	end
 end)

The code in cl_init.lua :


hook.Add( "HUDPaint", "robscoretext", function()
	local R = tostring ( RobScore )
	surface.SetFont( "CloseCaption_Bold" )
	surface.SetTextColor( 255, 255, 255, 255 )
	surface.SetTextPos( ScrW()/2 - 200, 20 ) 
	surface.DrawText( "Robbers: "..R )
end)

The kill command on the timer works but not the scoreboard update.
Thanks for any help!

not sure if this will fix your problem but try this:



hook.Add("Think", "FlagCheck", function()
   for _, flag in ipairs(ents.FindByClass("weapon_flag")) do
      if (!IsValid(flag)) then
         continue;
      end
      
      for _, ply in ipairs(ents.FindByClass("info_player_red")) do
         if (!IsValid(ply)) then
            continue;
         end
         
         if(ply:GetPos():Distance(flag:GetPos()) < 50) then
            RobScore = RobScore + 1;
            
            for k,v in pairs(player.GetAll()) do
               v:Kill();
            end
            
            break;
         end
      end
   end
end)


Doesn’t work, same problem. If I wasn’t clear on my first post when I begin the game the score correctly shows up on my HUD, 0 to 0. It just won’t update with the timer script in my first post.

Updated the script to teleport the entity away to see if the HUD was getting overloaded, but still does not work. Here is the updated script:


hook.Add("Think", "FlagCheck", function()

local entspawn = ents.FindByClass( 'info_player_blue' )[ 1 ]
local setpoint = entspawn:GetPos()
local flagent = ents.FindByClass( 'ctf_flag' )[ 1 ]

for _, flag in ipairs(ents.FindByClass("weapon_flag")) do
      if (!IsValid(flag)) then
         continue;
      end
      
      for _, ply in ipairs(ents.FindByClass("info_player_red")) do
         if (!IsValid(ply)) then
            continue;
         end
         
         if(ply:GetPos():Distance(flag:GetPos()) < 50) then
            RobScore = RobScore + 1;
            for k,v in pairs(player.GetAll()) do
               v:Kill();
            end
            flagent:SetPos( setpoint + Vector ( 40, 5, 0) )
            break;
         end
      end
   end
end)

Same problem occurs as stated above.

Okay, I think I have figured out the problem, the script is only updating the variable serverside, making the hud not update. If anyone has any solutions that would be great as just moving the script I posted above to shared.lua gives the error : [ERROR] gamemodes/car/gamemode/shared.lua:28: attempt to index local ‘entspawn’ (a nil value), and the score does not update giving us the original problem.

Yes, I was about to tell you 2 things:
-Don’t use think hook
-You are only updating variable serverside

[editline]28th June 2014[/editline]

Moving file to shared won’t make it update.

When a file is shared, all it’s variables and functions are accessible by both server and the client but if client changes the variable, it’ll only be changed clientside and it’s the same with server.

You need to use net library.

So, should I use a timer instead of the think function?