Adding & Removing Gear

Greetings,

I have a question, I tried looking for an answer for it but no answer was found. However, my question is that I want make a script to put it inside a part/brick which gives gear such as sword to the player who got on, not one player though but a bunch. Once they step off that part/brick the gear will be removed, if can, make the answer detailed so I can understand what’s going on inside the script, I am a starter.

(A gear from Avatar Shop, with ID)

Thanks!

1 Like

You could put a mesh in it and then put the tool in something like ServerStorage and clone it.

First of all, we need to take care of the sword. The idea you have is to import a sword that’s published using its ID, that’s possible using InputService, but a lot of problems would be encountered, mainly the loading time and sometimes the sword might even load and throws an error. Instead you can simply have a sword placed in ServerStorage (this place is used to store things not currently in need, that you will need later and you don’t want to be visible at the moment unless you want to) that will be :Clone()'d once the player steps in.

Now, for the step on a brick part, simply and without any introductions: the .Touched event. Whenever this event fires, we will check if the the that touched (which would be the event’s parameter) is the player’s character (keep in mind, character and not player, here is the difference); by checking if it has a humanoid, and if so, :Clone() the sword from server storage, and parent it to the character’s corresponding player object, using :GetPlayerFromCharacter(). To put it simply, the character is not the same as the player, the character is the physical representation of the player, the avatar that you see in workspace is the character, while the player object is found in Players service, and it’s an object that holds information concerning the player, such as his inventory or UIs. Technically, whatever hit the part and fired the event is the character, but the inventory is located in the player, so we need to get the player from the character using the :GetPlayerFromCharacter(). The folder found inside of the player that holds the inventory is called Inventory. Now for the removing part, there is a counter event for .Touched called .TouchEnded event, which fires whenever something stopped touching the connected part. It is important to say that these events aren’t super accurate but not a huge problem. When .TouchEnded is fired, check if the sword exists in the inventory, and also in the character (this is important, because when a tool is equipped, it’s transported from the inventory folder in player into the character’s model), and if so, :Remove().

2 Other things:

  1. You said you wanted this to work even if a lot of players are touching the part, that shouldn’t be a problem, since the event can fire multiple times it’s not limited to one time.
  2. It is important that you add an if statment for the first .Touched event checking if a sword already exists, else it will just keep on putting swords filling up the inventory. So, check if a sword exists in the inventory, and the character as I explained earlier
3 Likes

I recommend using :Clone() and :Destroy() instead of :Copy() and :Remove().

Oh yeah my super bad, idk what was in my head when I was writing this. No idea why I was using :Copy() xd