Skip to content

Getting started | Local player variables

Andrzej edited this page Jun 4, 2025 · 3 revisions

Why use local player variables?

While global variables are useful, they can sometimes lead to unexpected behavior in your scripts. Local player variables offer more control and predictability, making your scripts more robust.

How do local variables differ from global ones?

  1. They hold a snapshot of players provided. They won't automatically update, even when made from a global variable.
  2. They're accessible only in the script in which they were created.
  3. You can freely add or remove players from them.

How to create a local player variable?

Important

  • Variable name must start with a lowercase letter
  • Variable name can only be made up of letters and numbers
✅ @myPlayers
✅ @scp999
❌ @Idiot
❌ @999
❌ @i_love_vars

1) Use an existing global player variable

@myLocalVar = @scpPlayers

This will create @myLocalVar with the same players that @scpPlayers has in that moment!

2) Make an empty variable using ()

@myLocalVar = ()

Useful for adding players manually using a custom system.

3) Get a player value from a method

@myLocalVar = JoinPlayers @classDPlayers @scientistPlayers

This will get the player value from the JoinPlayers method.

Note

The use of methods as values will be explained later down the tutorial series. Don't worry about it for now, we will be coming back to this.

Solving problems with global variables

1) Global variables changing values mid script

  GiveItem @scpPlayers Radio
  Wait 1s
❗ DestroyItem @scpPlayers Radio

The issue presented here, where @scpPlayers can change its value because of the Wait 1s method can be remedied with local variables.

   @playersGivenRadio = @scpPlayers

   GiveItem @playersGivenRadio Radio
   Wait 1s
👍 DestroyItem @playersGivenRadio Radio

Now that we use the @playersGivenRadio variable, we are certain that all radios will be removed from player inventories.

2) No global variables that satisfy your needs

If you want to get all players from a room, e.g. EzGateA, you will need to check every single player's current room, and add them to your local variable accordingly.

@gateAPlayers = ()
for @plr in @allPlayers

 {*room} = Get @plr room
 if ({*room} == UNDEFINED)
  continue
 end

 {roomName} = RoomInfo {*room} name
 if ({roomName} != EzGateA)
  continue
 end
 
 @gateAPlayers = JoinPlayers @gateAPlayers @plr
end

In short, we have an empty variable called @gateAPlayers and we add players to that variable if their current room is EzGateA.

Note

The use of loops, conditions, literal variables and object references will be explained later down the tutorial series. Don't worry about it for now, we will be coming back to this.

Clone this wiki locally