GlobalInt not syncing between server and client


local Preparing = function()
    local fun_round_num
    if SERVER then
        SetGlobalInt("fun_round_num", math.random(#funrounds))
        fun_round_num = GetGlobalInt("fun_round_num",-1)
        print(fun_round_num)
    end
    hook.Remove("TTTBeginRound","BEGINFUNROUND")
    hook.Remove("TTTEndRound","ENDFUNROUND")
    hook.Remove("Think","FUNROUND_THINK")
    rounds_left = GetGlobalInt("ttt_rounds_left",6)-1
    if rounds_left == 0 then   
      fun_round_num = GetGlobalInt("fun_round_num",-1)
      print(fun_round_num)
      funrounds[fun_round_num]()
    end
  end

This is my piece of code from my addon for bonus fun rounds on the last round of each map in TTT. This hooks into TTTPrepareRound, and is supposed to get the server to pick a random bonus round from the table, send it to all the clients, and then run the function on both the client and server. I do this by setting a random int as a GlobalInt on the server, and then getting that on the client (to ensure it’s the same according to clients and servers), and calling the function at that index in the table. The problem is, that although the clients are always synced with each other (e.g their fun_round_num is the same), the server is sometimes different. I’ve tried to make sure it was syncing by setting the GlobalInt to -1 first on both client and server, and then trying to get the number again and again until it isn’t -1, but it still didn’t work. Does anyone have any idea of what to do?

-snip-

It does work, the problem for you is that it cannot sync it instantly, it takes a little bit of time to sync it, but in your case you are running the CLIENT code right after the SERVER sets it. Adding a timer of like a second works, but I don’t feel like that’s a very reliable way to do it. Just use net message.

I have tried what you said about the net messages and have replaced it with this code:


local Preparing = function()
    hook.Remove("TTTBeginRound","BEGINFUNROUND")
    hook.Remove("TTTEndRound","ENDFUNROUND")
    hook.Remove("Think","FUNROUND_THINK")
    rounds_left = GetGlobalInt("ttt_rounds_left",6)-1
    if true then--rounds_left == 0 then
      if SERVER then
        util.AddNetworkString("fun_round_num")
        net.Start("fun_round_num")
          fun_round_num = math.random(#funrounds)
          net.WriteInt(fun_round_num, 2)
          print("Debug - Net message sent: "..fun_round_num)
        net.Broadcast()
      end
      net.Receive("fun_round_num", function(length, server)
        fun_round_num = net.ReadInt(2)
        print("Debug - Net message received: "..fun_round_num)
      end)
      print("Debug - fun_round_num: "..fun_round_num)
      funrounds[fun_round_num]() --LINE 24 (THE ERROR LINE)
    end
  end

On the server I get the print “Debug - Net message sent: 1”
and “Debug - fun_round_num: 1”

However in the client I get a script error:

[ERROR] addons/redostrichttt_custom/lua/autorun/funrounds.lua:24: attempt to call a nil value

  1. fn - addons/redostrichttt_custom/lua/autorun/funrounds.lua:24
  2. Call - addons/ulib/lua/ulib/shared/hook.lua:184
    3. RoundStateChange - gamemodes/terrortown/gamemode/cl_init.lua:129
    4. func - gamemodes/terrortown/gamemode/cl_init.lua:204
    5. unknown - lua/includes/modules/net.lua:32

and the print “Debug - Net message received: 0”
as well as “Debug - fun_round_num: 0”

[editline]17th March 2015[/editline]

Just for clarification I am working on a project together with OP so you can treat me as OP as well.

Why not simply select the random round, then network that round id with the net-message you’re already broadcasting?

Those functions use umsg, and the order the data sends is: https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_channels_and_the_order_things_are_sent.lua – no matter what order you try to send, they’ll always send / receive net, u, con…

Using net-messages is a good idea, but also add that id. Alternatively, you could use my networking addon which rewrites those functions to use net, and my data / flag system… It is a drop-in system and will speed up networking / converts most of the umsg functions ( except things like SendLua / BroadcastLua, etc… ) to net ( the rest will be converted soon )…

Yeah, that should work. But really, is there anything you haven’t made an addon for, Acecool?:v:

But… I am networking the round ID, am I not?

[editline]17th March 2015[/editline]

Or am I misunderstanding something

Edited code from earlier.
The server is now not running the functions…


local funrounds = {}
  if SERVER then
    util.AddNetworkString("fun_round_num")
  end
  
  local Preparing = function()
    hook.Remove("TTTBeginRound","BEGINFUNROUND")
    hook.Remove("TTTEndRound","ENDFUNROUND")
    hook.Remove("Think","FUNROUND_THINK")
    rounds_left = GetGlobalInt("ttt_rounds_left",6)-1
    if true then--rounds_left == 0 then
      if SERVER then
        net.Start("fun_round_num")
          fun_round_num = math.random(#funrounds)
          net.WriteInt(fun_round_num, 2)
          print("Debug - Net message sent: "..fun_round_num)
        net.Broadcast()
      end
      
    end
  end
  
  net.Receive("fun_round_num", function(length, server)
    fun_round_num = net.ReadInt(2)
    print("Debug - Net message received: "..fun_round_num)
    funrounds[fun_round_num]()
  end)