F to pick up item

I want to make a system where I press F to pick up a item and it goes into my custom Inventory


Layout:
image
I want the item to go into the grid here but i also want the item to also get a image from Replicated storage.
image

I want the item to go into the Pocket section of my inventory, but if the image is too big then it goes into the Pouch Section. Can someone please help me with this?

2 Likes

Do you have any pre-made scripts done yet and by any chance what solutions have you tried? Anyway, I can explain to you how you can create it if you don’t have anything done yet. First, you will have to constantly check the magnitude of player and item so we will know if the player is close enough so he can pick it up. Once he is in range he presses F which you can use UserInputService and you are going to be able to find if the key pressed is equalled to F. Once this is correct you have to change the parent of the item inside your grid and that’s it.

That’s kinda confusing, can you explain with a little more detail after F is pressed?

You have to use UserInputService and use the InputBegan Event

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,chat)
     if input.KeyCode == Enum.KeyCode.E and not chat then
         --//Check what tool it is and assign it in the inventory respectively
    end
end)
1 Like

How to find key pressed @UniversalScripter already mentioned. I have a question for you, do you have any max size of image before limit it’s too big and if you are planning to check the size of image by image size property? Because if that’s the thing then all you have to do it add one more if statement to check if value is above or below limit and then you simple change the parent of item you was close to.

I guess i’d go about something like this, feel free to ask questions if you dont understand :slight_smile:

local UIS = game:GetService("UserInputService")
local Replicated = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local ItemsFolder = --put here the folder from the workspace where all pickable items will be
local Inventory = player.PlayerGui.BackPack.BackPack.Inventory.InventoryScroll.Pouch.Grid
local Character = player.Character
local debounce = false
local MaxDist = 50

UIS.InputBegan:Connect(function(input,chat)
     if input.KeyCode == Enum.KeyCode.E and not chat and not debounce then
          debounce = true
          local Distance = MaxDist + 1
          local ItemSelected = nil
          for _, Item in pairs (ItemsFolder:GetChildren()) do
               if (Item.Position - Character.HumanoidRootPart.Position).Magnitude < Distance then
                    ItemSelected = Item
                    Distance = (Item.Position - Character.HumanoidRootPart.Position).Magnitude
               end
          end
          if ItemSelected then
               for _, Slot in pairs (Inventory:GetChildren()) do
                    if not Slot:FindFirstChildOfClass("Image") then -- there is some other ways around if you need to have multiple images
                         local Image = Replicated.Item:FindFirstChild(ItemSelected.Name)
                         If Image then
                              local NewImage = Image:Clone()
                              NewImage.Parent = Slot
                              ItemSelected:Destroy() -- if FE send info to server to delete the object
                         else
                              print("error, couldn't find object in replicated"..ItemSelected.Name)
                         end
                    return
               end
          end
          debounce = false
     end
end)

I don’t want the item to go into the slot because the slots are 50x50 and some of my items are 100x50 meaning they won’t fit, i want to be able to drag them around, how would i do this?

You could create a 2d array and assign values based on if there is an item there or not

1,1,0,0
1,1,0,0
0,0,0,0

This is an inventory with a 2x2 item in the top left corner, for example.

How would i assign values and stuff, this is confusing me

Each frame that you have inside of an inventory frame would represent a position in the table. Upon clicking on one of the slots inside that inventory container, you need to figure out if it is taken or not. The way you can do this is by doing a for loop, starting at the position clicked, through the 2d array table which represents the container. You would go down from however many slots high the item is, and then to the left/right (depending on rotation).

0,0,0
0,0,0
0,1,1

In this case, this represents a 3x3 container which has a 2x1 item in the bottom right. Let’s say you wanted to place an item in the top right. This item is 2x2. You would go two to the right of where the mouse clicked, which would result in a zero. Then you would go down 2 from where the user clicker, which also results in 0. In this case it is free, so the user can place. You would probably also want to go down and to the right, this is for if you plan to have items that don’t have a rectangular shape, though I don’t think this would be the case.

1 Like

this is really confusing, how would i make a 2d array table?

This should give you all the information you need :slight_smile: Programming in Lua : 11.2

Im trying to make an inventory like the game Unturned

1 Like