Page 1 of 1

Cannot add Player to Force on first Join

Posted: Wed Sep 13, 2023 9:26 pm
by AaronVB
Hi, I'm currently making my first mod. It uses the on_player_joined_game event to assign players to a team/force on join.
This is my code:

Code: Select all

local function onPlayerJoin(event)
  print("on join called")
  local force =  nil
  
  for force_index = 1, #game.forces do
    if game.forces[force_index ].name == "test" then
      force = game.forces[force_index]
    end
  end

   if force == nil then
     force = game.create_force("test")
   end

  game.players[event.player_index].force = force
end
I do get the "on join call" output in my server console, yet /c print(tostring(game.get_player("AaronVB").force.name)) returns "player" until i leave and join the server again. does anyone have a clue? I've tried just calling game.players[event.player_index].force = force twice, but no luck

EDIT:
Here is what the output on my console look like:

Code: Select all

2023-09-13 23:13:39 [JOIN] AaronVB joined the game
2023-09-13 23:13:44 [COMMAND] AaronVB (command): print(tostring(game.get_player("AaronVB").force.name))
player
2023-09-13 23:14:47 [LEAVE] AaronVB left the game
2023-09-13 23:14:48 [JOIN] AaronVB joined the game
2023-09-13 23:14:53 [COMMAND] AaronVB (command): print(tostring(game.get_player("AaronVB").force.name))
1