How can I get the "index" of the current character in "script. On_init"

Place to get help with not working mods / modding interface.
Post Reply
sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

How can I get the "index" of the current character in "script. On_init"

Post by sdgmlj »

This is included in multiplayer games. How can I get it? thank you

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: How can I get the "index" of the current character in "script. On_init"

Post by robot256 »

Mod scripts run at the game level, and execute the same code on every player's machine. They can access every player's data all the time.

To find every player's characters, use:

Code: Select all

for index, player in pairs(game.players) do
  local character = player.character
  if character ~= nil then
    -- Do stuff with character
  end
end

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: How can I get the "index" of the current character in "script. On_init"

Post by sdgmlj »

robot256 wrote:
Mon May 23, 2022 1:25 am
Mod scripts run at the game level, and execute the same code on every player's machine. They can access every player's data all the time.

To find every player's characters, use:

Code: Select all

for index, player in pairs(game.players) do
  local character = player.character
  if character ~= nil then
    -- Do stuff with character
  end
end
Sorry, I don't quite understand. I just want to find my own "index". How should I write it?
For example, there are five people in the game, and I just want to know my own "index"

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: How can I get the "index" of the current character in "script. On_init"

Post by robot256 »

The mod doesn't know whose computer it is running on--it runs on everyone's simultaneously. There is no "me", there is only player 1, player 2, etc. The mod can receive events every time any player does something like open a GUI or press a key, with a link to the player that did it. There are also "runtime per player" mod settings, which the player changes in the mod settings window and the mod can read for each player.

Typically, mods that need to do something different for a particular player keep a table in global with the unique data for each player. This includes settings, handles to custom GUIs, etc. Then whenever a particular player triggers an action, the mod can decide what to do, where to pick up where it left off for that player, etc.

What do you need to do once you find the character you want?

You will need to have a control of some sort to tell your mod that "this player" wants something done. That is how it will know which player is "me". You will have to write it so that it could be any player, or all of them at once.

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: How can I get the "index" of the current character in "script. On_init"

Post by sdgmlj »

robot256 wrote:
Mon May 23, 2022 3:29 am
The mod doesn't know whose computer it is running on--it runs on everyone's simultaneously. There is no "me", there is only player 1, player 2, etc. The mod can receive events every time any player does something like open a GUI or press a key, with a link to the player that did it. There are also "runtime per player" mod settings, which the player changes in the mod settings window and the mod can read for each player.

Typically, mods that need to do something different for a particular player keep a table in global with the unique data for each player. This includes settings, handles to custom GUIs, etc. Then whenever a particular player triggers an action, the mod can decide what to do, where to pick up where it left off for that player, etc.

What do you need to do once you find the character you want?

You will need to have a control of some sort to tell your mod that "this player" wants something done. That is how it will know which player is "me". You will have to write it so that it could be any player, or all of them at once.
Thank you. I seem to suddenly understand that I just want to name some variables related to index, such as global Mods [player_index], I think it would be more appropriate for me to add it to "on_player_created". Thank you, I finally understand

Post Reply

Return to “Modding help”