Give item to a player with chat

can i give items to a player using chat like this

?give (player name) (item quantity) (item)

the part of the item quantity dont need to be added on the script its just optional

1 Like

Yes, you can loop through all item types in the game to find an item type whose name matches the one specified in your command. Then, loop through all players in human players to find a player whose name matches the name specified. Finally, give the selected item type to the unit owned by the selected player.

Here is a script you could use.


It triggers when a player sends a chat message. First it checks that the name of the player sending the chat message is the name of an admin, then checks that the message begins with /give, then searches for an item type which the message ends in the name of, then searches for a player whose name equals the part of the message between "/give " and the item name, loops through units owned by selected player, and gives selected item type to selected unit.

Script Raw JSON
{
     "triggers": [
          {
               "type": "playerSendsChatMessage"
          }
     ],
     "actions": [
          {
               "type": "condition",
               "conditions": [
                    {
                         "operandType": "boolean",
                         "operator": "=="
                    },
                    {
                         "function": "getPlayerName",
                         "entity": {
                              "function": "getTriggeringPlayer"
                         }
                    },
                    "kyle69"
               ],
               "then": [
                    {
                         "type": "condition",
                         "conditions": [
                              {
                                   "operandType": "boolean",
                                   "operator": "=="
                              },
                              {
                                   "function": "stringStartsWith",
                                   "sourceString": {
                                        "function": "getLastChatMessageSentByPlayer",
                                        "player": {
                                             "function": "getTriggeringPlayer"
                                        }
                                   },
                                   "patternString": "/give "
                              },
                              true
                         ],
                         "then": [
                              {
                                   "type": "forAllItemTypes",
                                   "itemTypeGroup": {
                                        "function": "allItemTypesInGame"
                                   },
                                   "actions": [
                                        {
                                             "type": "condition",
                                             "conditions": [
                                                  {
                                                       "operandType": "boolean",
                                                       "operator": "=="
                                                  },
                                                  {
                                                       "function": "stringEndsWith",
                                                       "sourceString": {
                                                            "function": "getLastChatMessageSentByPlayer",
                                                            "player": {
                                                                 "function": "getTriggeringPlayer"
                                                            }
                                                       },
                                                       "patternString": {
                                                            "function": "getItemTypeName",
                                                            "itemType": {
                                                                 "function": "selectedItemType"
                                                            }
                                                       }
                                                  },
                                                  true
                                             ],
                                             "then": [
                                                  {
                                                       "type": "forAllPlayers",
                                                       "playerGroup": {
                                                            "function": "humanPlayers"
                                                       },
                                                       "actions": [
                                                            {
                                                                 "type": "condition",
                                                                 "conditions": [
                                                                      {
                                                                           "operandType": "boolean",
                                                                           "operator": "=="
                                                                      },
                                                                      {
                                                                           "function": "substringOf",
                                                                           "string": {
                                                                                "function": "getLastChatMessageSentByPlayer",
                                                                                "player": {
                                                                                     "function": "getTriggeringPlayer"
                                                                                }
                                                                           },
                                                                           "fromIndex": 8,
                                                                           "toIndex": {
                                                                                "function": "calculate",
                                                                                "items": [
                                                                                     {
                                                                                          "operator": "-"
                                                                                     },
                                                                                     {
                                                                                          "function": "getLengthOfString",
                                                                                          "string": {
                                                                                               "function": "getLastChatMessageSentByPlayer",
                                                                                               "player": {
                                                                                                    "function": "getTriggeringPlayer"
                                                                                               }
                                                                                          }
                                                                                     },
                                                                                     {
                                                                                          "function": "calculate",
                                                                                          "items": [
                                                                                               {
                                                                                                    "operator": "+"
                                                                                               },
                                                                                               1,
                                                                                               {
                                                                                                    "function": "getLengthOfString",
                                                                                                    "string": {
                                                                                                         "function": "getItemTypeName",
                                                                                                         "itemType": {
                                                                                                              "function": "selectedItemType"
                                                                                                         }
                                                                                                    }
                                                                                               }
                                                                                          ]
                                                                                     }
                                                                                ]
                                                                           }
                                                                      },
                                                                      {
                                                                           "function": "getPlayerName",
                                                                           "entity": {
                                                                                "function": "selectedPlayer"
                                                                           }
                                                                      }
                                                                 ],
                                                                 "then": [
                                                                      {
                                                                           "type": "forAllUnits",
                                                                           "unitGroup": {
                                                                                "function": "allUnitsOwnedByPlayer",
                                                                                "player": {
                                                                                     "function": "selectedPlayer"
                                                                                }
                                                                           },
                                                                           "actions": [
                                                                                {
                                                                                     "type": "giveNewItemToUnit",
                                                                                     "itemType": {
                                                                                          "function": "selectedItemType"
                                                                                     },
                                                                                     "unit": {
                                                                                          "function": "selectedUnit"
                                                                                     }
                                                                                }
                                                                           ]
                                                                      },
                                                                      {
                                                                           "type": "break"
                                                                      }
                                                                 ],
                                                                 "else": []
                                                            }
                                                       ]
                                                  },
                                                  {
                                                       "type": "break"
                                                  }
                                             ],
                                             "else": []
                                        }
                                   ]
                              }
                         ],
                         "else": []
                    }
               ],
               "else": []
          }
     ],
     "name": "/give <player> <item>"
}
2 Likes