Find a certain slot in inventory?

Howdy.

I am finding it difficult to learn how to make a script where the script identifies a certain slot in the players inventory. Similar to what piggy uses for their keys etc…

Please, any help on how to find a certain slot and switch tools like piggy does?

2 Likes

Not sure who piggy is or whatever their keys are, but it sounds like you’re trying to find an index within a table.

1 Like

Piggy is the new popular game.

Please explain more how you want your system to work, unfortunately, not all of us here on the DevForum is aware of that game.

I’am aware, basically, in piggy, you can collect only one item per time, if you grab other, it will throw the one you were using right now, letting you have only 1 inventory slot.

if you mean you want to find the index or key that corresponds with a certain value, then you can get the index this way.

 function getIndex(value, array)
             for k, v in pairs(array) do
                  if v == value then
             return k end
       end 
  end

    tab = {"a",5,2,"str"}
 
   print(getIndex("str", tab)) --> 4
   print(getIndex(5, tab)) --> 2

This sort of mechanism.

So if I’m aware what you’re wanting is a script that can switch out the tool that is in your inventory with the one that you clicked?

1 Like

Correct. I don’t know where to begin though.

But only where to being. Not a full script.

if you want it so there can only be one item in the inventory, and when there’s more the previous one is removed, do this:

   local Players = game:GetService("Players")
   local player = Players.LocalPlayer
 
   -- Connect to MouseClick
   local inv = player:WaitForChild("Backpack") 
 
   if  #inv:GetChildren() ~=0 then 
       return inv:GetChildren()[1]:Destroy()
   end
 print("no tools found ")

Connect the MouseClick event and then just add clone the other tool to the backpack

4 Likes

If I understand correctly, you’re trying to swap out Red Key with Blue Key.

In that case the algorithm you’re looking for is pretty simple, something like this:

part.Clicked:Connect(function()
    if player owns red key then
       take away the red key
       give player blue key
    end 
end

I’am not the OP, i didn’t ask for that, but i guess that will be good for him.(The OP.)

1 Like

Use a ClickDetector’s .MouseClick event and from the player delete any instance that is under the player’s Backpack and add a new one.

local ExistingTool = Player.Backpack:GetChildren()[1]
if ExistingTool then
     ExistingTool:Destroy()
end
-- Give the new tool to the player via their Backpack.
1 Like

Never mind! It has been solved!

1 Like

How would you replace the position though?

If you’re talking about how to replace the position of the new tool in your backpack to the old one you’re likely better off creating your own backpack system to do this.

There is one way I can think of doing it with Roblox’s current backpack UI but it will be very messy and horrible for performance so it is highly not recommended. You could possibly sort a table with the tool indexes before removing it and replace the old tool index with the new one, then set the parent of all the tools to nil on the server and set their parent back in order with the sorted table.

I’ve figured it out already thanks though!