1.  ![Epic Games](https://edc-cdn.net/assets/images/logo-epic.svg)[Developer](/)
2.  [Documentation](/documentation/ "Documentation")
3.  Fortnite
    *   [Unreal Engine](/documentation/en-us/unreal-engine)
    *   [Fortnite](/documentation/en-us/fortnite)
    *   [MetaHuman](/documentation/en-us/metahuman)
    *   [Twinmotion](/documentation/en-us/twinmotion)
    *   [RealityScan Mobile](/documentation/en-us/realityscan-mobile)
    *   [Fab](/documentation/en-us/fab)
4.  [Fortnite Documentation](/documentation/en-us/fortnite/fortnite-documentation "Fortnite Documentation")
5.  [Build a Game](/documentation/en-us/fortnite/build-a-game-in-unreal-editor-for-fortnite "Build a Game")
6.  [Triad Infiltration](/documentation/en-us/fortnite/triad-infiltration-in-verse "Triad Infiltration")
7.  4\. Granting Weapons on Player Spawn

4\. Granting Weapons on Player Spawn
====================================

Grant players weapons when they spawn in a match.

![4. Granting Weapons on Player Spawn](https://uefn-docs.yuzulabs.dev/community/api/documentation/image/0c48a800-8166-4703-bd65-a7c01afefba5?resizing_type=fill&width=1920&height=335)

###### Prerequisite topics

In order to understand and use the content on this page, make sure you are familiar with the following topics:

*   [3\. Balancing Teams Asymmetrically](/documentation/en-us/fortnite/triad-infiltration-03-balancing-teams-asymmetrically-in-verse)

On this page

Now that you’ve balanced players into teams, you want to grant them the correct weapons based on what team they were balanced onto. Follow the steps below to learn how to grant players the appropriate weapons when they spawn.

Granting Weapons based on Team
------------------------------

1.  In the `triad_infiltration_game` class definition, add a new function named `GrantTeamWeapon()`. This function grants a weapon to a player based on their respective team. `GrantTeamWeapon(InPlayer:player):void=`
    
2.  In `GrantTeamWeapon()`, get the team for the given player. Then in a `for` loop, iterate through each team in the `Teams` array, getting the index for that team and storing it in a variable `TeamIndex`. Check if the given player’s team matches this team as a filter condition in your `for` loop.
    
    Verse
    
         `GrantTeamWeapon(InPlayer:player):void=          if(CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam[InPlayer]):              for(TeamIndex -> PlayerTeam:Teams, PlayerTeam = CurrentTeam):`
    
    GrantTeamWeapon(InPlayer:player):void= if(CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam\[InPlayer\]): for(TeamIndex -> PlayerTeam:Teams, PlayerTeam = CurrentTeam):
    
    Copy full snippet(3 lines long)
    
3.  Since your filter condition will ensure the code within the `for` loop runs with the correct team, retrieve the appropriate item granter for that team by indexing into `WeaponGranters` using the `TeamIndex` of that team. Finally, call `GrantItem()` using the given player. Your `GrantTeamWeapon()` code should look like:
    
    Verse
    
         `# Grants players a weapon based on the index of their team in the Teams array      # by indexing into the WeaponGranters array.      GrantTeamWeapon(InPlayer:player):void=          if(CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam[InPlayer]):              for(TeamIndex -> PlayerTeam:Teams, PlayerTeam = CurrentTeam):                  if(WeaponGranter := WeaponGranters[TeamIndex]):                      WeaponGranter.GrantItem(InPlayer)                      Logger.Print("Granted a Player on team {TeamIndex + 1} a weapon")`
    
    \# Grants players a weapon based on the index of their team in the Teams array # by indexing into the WeaponGranters array. GrantTeamWeapon(InPlayer:player):void= if(CurrentTeam := GetPlayspace().GetTeamCollection().GetTeam\[InPlayer\]): for(TeamIndex -> PlayerTeam:Teams, PlayerTeam = CurrentTeam): if(WeaponGranter := WeaponGranters\[TeamIndex\]): WeaponGranter.GrantItem(InPlayer) Logger.Print("Granted a Player on team {TeamIndex + 1} a weapon")
    
    Copy full snippet(8 lines long)
    
    Make sure that the order of your teams in `Teams` matches the order of your item granters in `WeaponGranters`. If the Infiltrators are Team 1, then the first granter in `WeaponGranters` should be for the Infiltrators. Double-check in the editor that these values are correct.
    

Granting Weapons When Players Spawn
-----------------------------------

1.  In the `triad_infiltration_game` class definition, add a new function `OnPlayerSpawn()`. This function takes an `agent` and uses it to call `GrantTeamWeapon()` to grant the appropriate weapon to the player. `OnPlayerSpawn(SpawnedAgent:agent):void=`
    
2.  In `OnPlayerSpawn()`, cast the `SpawnedAgent` to a `player`. Then call `GrantTeamWeapon()` passing the player. Your `OnPlayerSpawn()` function should look like:
    
    Verse
    
         `# Runs when any player spawns from a spawn pad.      # Calls GrantTeamWeapon using the provided SpawnedAgent.      OnPlayerSpawn(SpawnedAgent:agent):void=          if(SpawnedPlayer := player[SpawnedAgent]):              Logger.Print("Attempting to grant spawned player a weapon")              GrantTeamWeapon(SpawnedPlayer)`
    
    \# Runs when any player spawns from a spawn pad. # Calls GrantTeamWeapon using the provided SpawnedAgent. OnPlayerSpawn(SpawnedAgent:agent):void= if(SpawnedPlayer := player\[SpawnedAgent\]): Logger.Print("Attempting to grant spawned player a weapon") GrantTeamWeapon(SpawnedPlayer)
    
    Copy full snippet(6 lines long)
    
3.  In `OnBegin()`, before the call to `BalanceTeams()`, subscribe each player spawner’s `SpawnedEvent` using a `for` loop to the `OnPlayerSpawn()` function you just defined.
    
    Verse
    
             OnBegin<override>()<suspends>:void =
                 # Get all the Teams
                 set Teams = GetPlayspace().GetTeamCollection().GetTeams()
                 # Save the teams to later reference them
                 set InfiltratorsOpt = option{Teams[0]}
                 set AttackersOpt = option{Teams[1]}
                 set DefendersOpt = option{Teams[2]}
                 if:
                     Infiltrators := InfiltratorsOpt?
                     Attackers := AttackersOpt?
        
    
    Expand codeCopy full snippet(24 lines long)
    
4.  Save the script, build it, and click **Launch Session** in the UEFN toolbar to playtest the level. When you playtest your level, each player should end up on the team with the largest difference, and should spawn with an appropriate weapon for their team. Verify this behavior using the log.
    
    [![Granting Weapons On Spawn](https://uefn-docs.yuzulabs.dev/community/api/documentation/image/461dc4f4-4462-402f-8970-db51aec374ba?resizing_type=fit)](https://uefn-docs.yuzulabs.dev/community/api/documentation/image/461dc4f4-4462-402f-8970-db51aec374ba?resizing_type=fit)
    

Next Step
---------

In the [next step](https://uefn-docs.yuzulabs.dev/documentation/en-us/fortnite/triad-infiltration-making-players-invisible-in-verse) of this tutorial, you'll learn how to make the Infiltrators invisible when they spawn and when the game begins.

*   [verse](https://uefn-docs.yuzulabs.dev/community/search?query=verse)
*   [map](https://uefn-docs.yuzulabs.dev/community/search?query=map)
*   [multiplayer](https://uefn-docs.yuzulabs.dev/community/search?query=multiplayer)
*   [option](https://uefn-docs.yuzulabs.dev/community/search?query=option)
*   [team](https://uefn-docs.yuzulabs.dev/community/search?query=team)
*   [playspace](https://uefn-docs.yuzulabs.dev/community/search?query=playspace)

* * *

Ask questions and help your peers [Developer Forums](https://forums.unrealengine.com/categories?tag=fortnite)

Write your own tutorials or read those from others [Learning Library](https://uefn-docs.yuzulabs.dev/community/fortnite/learning)

On this page

*   [Granting Weapons based on Team](/documentation/en-us/fortnite/triad-infiltration-04-granting-weapons-on-player-spawn-in-verse#granting-weapons-based-on-team)
*   [Granting Weapons When Players Spawn](/documentation/en-us/fortnite/triad-infiltration-04-granting-weapons-on-player-spawn-in-verse#granting-weapons-when-players-spawn)
*   [Next Step](/documentation/en-us/fortnite/triad-infiltration-04-granting-weapons-on-player-spawn-in-verse#next-step)