Need help with this inventory code

I want it so the object named Cup once clicked goes into the players inventory

The issue is that when i click the Cup model nothing happens and the item doesn’t go into the inventory

I’ve tried looking up online and rewriting the code twice but nothing seems to work

Heres the code i have at the moment the cups script and the inventory script:

local cup = script.Parent
local clickDetector = cup:FindFirstChild("ClickDetector")

local function onClicked(player)
	local backpack = player:FindFirstChild("Backpack")
	if backpack then
		local cupClone = cup:Clone()
		cupClone.Parent = backpack
		cupClone.Name = "Cup"
		cupClone.Anchored = false
		cupClone.CanCollide = false
		cup:Destroy()
	end
end

clickDetector.MouseClick:Connect(onClicked)
local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")

local function showInventory()
	for _, item in pairs(backpack:GetChildren()) do
		print("Inventory Item: " .. item.Name)
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.I then
		showInventory()
	end
end)

2 Likes

Add a print statement inside the onClicked function to see if it’s being called when you click the Cup.

1 Like

Is the cup item you are cloning a tool and not a model? It needs to be a tool for it to properly work.

1 Like

how could i convert it fast from model to tool?

1 Like

You can’t, just make another item in replicated storage, clone that instead of the cup model.

1 Like

I wouldn’t give the player a tool locally. You can make a tool and then insert the model into that… simply rename the man part of it to Handle and make that the primary part of the tool. Then weld the stuff together. Boom. Got a tool.

You could use a class converter plugin but that is only in studio and NOT usable ingame.

So do i make 1 cup a tool and another a model or just 1 tool cup

I’d suggest watching basic tutorials before posting on the forum… there’s many videos that show how to make tools, where to store them, how to use them, make custom ones, etc.

2 cup models, 1 item, 1 model that the player can click to get into their inventory, Alternatively you could just use roblox’s default pick up system and just put the tool on the ground then when the player touches it it goes into their inventory.

Thing is i dont want 1 person to just have that item.

This cup will be placed on a counter and anyone who clicks it gets it into their inventory

1 Like

As I said, just make a tool in replicated storage, copy and paste whatever is inside the cup expect the script, then change your script to clone the cup from replicated storage, Another issue with your code if this did work was that the cup would have the script in it so other players could click the cup in another players hand and get it.

how would i fix that error when a player steals

do not include the script inside replicatedstorage

Also when reading your code, you print the players items when they try to open their inventory, You do know that the player cannot see print(), Right? Print() is just for debugging and outputting information in the console.

Can you provide the tool as a place file? It would be easier to help.

how would i do that?
im still new to scripting so im sort of clueless here

Documentation - Roblox Creator Hub figure it out yourself, we cannot help you if you cannot figure out or don’t know anything.

Hello!

Is your Cup a tool or a part? If it’s a part, try this. Parent it to your ClickDetector and create a copy of your tool and put it in ServerStorage.

script.Parent.MouseClick:Connect(function(Player)
	local Cup = game:GetService("ServerStorage"):FindFirstChild("Cup"):Clone()
	tool.Parent = Player.Backpack
end)

Change your LocalScript to this:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local function ShowInventory()
	print("function correctly called")
	
	for _, item in pairs(Character:GetChildren()) do
		if item:IsA("Tool") then
			print("Inventory item: " .. item.Name)
		else
			print("No items found :(")
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, GPE)
	if not GPE and input.KeyCode == Enum.KeyCode.E then
		print("E was clicked")
		ShowInventory()
	end
end)

Hope this helps! :slight_smile:

1 Like

where does the first and second code go? also should i convert the model to a tool? also should the one in workspace be model or tool? cuz u only mentioned to dupe it and put in server storage. But im guessing it should be tool in both work space and serverstorage

1 Like