Scripting Questions

I have some questions about scripting, can someone answer them for me?

  1. ipairs vs. pairs, does it matter which one I use?

  2. do for loops do it one by one, for example if i did:

for i, plr in pairs(game.Players:GetChildren) do
wait(1)
print(plr)
end

would it wait 1 then print a name then wait then print a name?

  1. So I have leaderstats, but it uses .PlayerRemoving to save them, which I heard is deprecated and doesn’t always run. What can I do instead of .PlayerRemoving?
1 Like
  1. ipairs is meant for iterating through arrays, so only tables that use numerical indices starting at one. pairs can do the same thing as ipairs but also lets you iterate through dictionaries, with indices that aren’t necessarily numbers, although the order in which it traverses key-value pairs might not be predictable.

  2. Your example will wait one second, then print a player’s name, then wait another second, then print the next player’s name, and so on.

  3. I don’t remember anyone saying that the PlayerRemoving event is going to be deprecated, so it should be safe to use.

2 Likes

Hmm, I thought I heard somewhere that PlayerRemoving was deprecated, maybe not then. Thanks for the information! :smile:

adding on to what @Blokav said, (question 3) PlayerRemoving isn’t deprecated, but it does only run if the player leaves. So use game:BindToClose to check if the game is shutdown etc. Check it out here.

1 Like

While ipairs isn’t commonly used nowadays, some people still prefer it over pairs because of its performance compared to the regeular pairs. Also, you should just be using the method :GetPlayers() instead of :GetChildren().

The PlayerRemoving event is not going to be deprecated. If it were to be deprecated, then there is going to be no way effective way of detecting when a player has left, other than calling “ChildRemoved” event on the Players service, which I am not really fond of, which is why the PlayerRemoving event will strictly not be deprecated - name changes and how it functions, or the parameters it brings are the only changes that could happen.

Though you should take into account that :GetChildren() and :GetPlayers() both function similarly, and it does not somewhat affect readability in any way possible. So if PlayerRemoving were to actually be deprecated, and there were no replacements at the moment, you can then call the ChildRemoved event on the Players service which should more than likely work the same as PlayerRemoving.

1 Like