Problem with custom inventory system

So I have this script:

player.Character.Humanoid:EquipTool(player.Backpack["slot"..key.ThingOccupying.Value])

“key” is the number that the player pressed, and ThingOccupying is a string value that contains the name of the tool occupying that slot. The images that represent the slots are named slot1,slot2,slot3, etc.
However, this line gives me an error:

 Players.nicknickphoenix.PlayerGui.ScreenGui.LocalScript:43: attempt to index nil with 'Value'

How should I fix it so that it will equip the tool that is supposed to be equipped?

2 Likes

The error suggests that ThingOccupying is nil .

Also, you shouldn’t equip tools on the client due to replication issues. Try using a RemoteEvent to tell the server to equip the tool.

1 Like

So, “key” is the number that the player pressed. What exactly is it? Is it a NumberValue or Enum.KeyCode or something else?

We need to know this, since the only thing the error message is saying is that "there is no ‘ThingsOccupying’ inside of ‘key’

1 Like

I used

local mouse = player:GetMouse()
	 
mouse.KeyDown:connect(function(key)

to get the key

Oh yeah, forgot about that however ThingOccupying shouldn’t be nilScreen Shot 2021-08-07 at 12.41.01 PM
(As you can see, it’s a string value inside all the slots)

In this case, “key” is either something like MouseButton1 or an enum thing idk, but it does not contain ThingsOccupying for sure.

1 Like

when I do

print("slot"..key)

In the output, I get “slot1”, which is the name of the image button and slot1.ThingOccupying.Value should be the name of the tool, but instead it says “attempt to index nil with ‘Value’”

Oh I think I got it. It’s a simple logic error. This should maybe work:

player.Character.Humanoid:EquipTool(player.Backpack[("slot"..key).ThingOccupying.Value])

Replace your script’s line that caused the error with this.

Thanks, however it still did not work

Did you get any errors? If yes please tell me what the error was.

I just got the same error again

Send me a screenshot of the player’s backpact in Explorer please.

Actually I managed to fix it, I changed the line to

player.Character.Humanoid:EquipTool(player.Backpack[player.PlayerGui.ScreenGui["slot"..key].ThingOccupying.Value])
2 Likes