Am trying to make Inventory system and it get error on Itemdrop System
Error: attempt to index nil with ‘Clone’
I checked codes lots of times but i couldn’t find the reason
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DropItem = ReplicatedStorage:WaitForChild("DropItem")
script.Parent.InventoryMom.InventoryGui.Templates.Item.Visible = false
wait()
local player = game.Players.LocalPlayer
local Inventory = player.Inventory
local MainGui = script.Parent
local InventoryGui = MainGui.InventoryMom.InventoryGui
for i, ItemButton in pairs(InventoryGui.ItemList:GetDescendants()) do
if ItemButton:IsA("ImageButton") then
ItemButton.MouseButton2Up:Connect(function()
local itemFrame = ItemButton.Parent
local itemValue = Inventory:FindFirstChild(itemFrame.Name)
if itemValue.Value > 0 then
local DropItem = DropItem:InvokeServer(itemFrame.Name) -- Item drop!
if DropItem == true then
if itemValue.Value > 0 then
itemFrame.ItemQuantity.Text = itemValue.Value
else
itemFrame.Visible = false
end
end
end
end)
end
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Items = ReplicatedStorage:WaitForChild("Items")
local DropItem = ReplicatedStorage:WaitForChild("DropItem")
DropItem.OnServerInvoke = function(player, itemName) --receive
local Inventory = player.Inventory
local item = Inventory:FindFirstChild(itemName)
if item then
if item.Value > 0 then
item.Value = item.Value - 1
local itemClone = Items:FindFirstChild(itemName):Clone()
itemClone.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 6
itemClone.Parent = game.Workspace
return true
else
return false
end
end
end
Could you possibly provide us with the output (that includes the error)? If we can identify the line of code where this occurs, it would be much easier. Cheers!
From what I can assume, the server cannot find the item you are looking for. You are using FindFirstChild() on the item name, so if the server does not find it, it is trying to clone something nil. Try checking if the item exists first.
Reference: local itemClone = Items:FindFirstChild(itemName):Clone()
This could be for two likely reason:
You are searching in the wrong directory or the item just doesn’t exist
The item exists in the client but not the server and therefore the server cannot see it
The error seems to be occuring because itemName is not a child of the Items (folder I’m assuming) inside of ReplicatedStorage.
You can check this by adding an if statement like so:
if item.Value > 0 and Items:FindFirstChild(itemName) then -- so we know it exists
item.Value = item.Value - 1
local itemClone = Items[itemName]:Clone()
itemClone.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 6
itemClone.Parent = game.Workspace
return true
else
return false
end
ServerScriptService.RemoteHandler:42: attempt to index nil with ‘Clone’
for i, ItemButton in pairs(InventoryGui.ItemList:GetDescendants()) do
if ItemButton:IsA("ImageButton") then
ItemButton.MouseButton2Up:Connect(function()
local itemFrame = ItemButton.Parent
local itemValue = Inventory:FindFirstChild(itemFrame.Name)
if itemValue.Value > 0 then
local DropItem = DropItem:InvokeServer(itemFrame.Name) -- This line
if DropItem == true then
if itemValue.Value > 0 then
itemFrame.ItemQuantity.Text = itemValue.Value
else
itemFrame.Visible = false
end
end
end
end)
end
end
DropItem.OnServerInvoke = function(player, itemName)
local Inventory = player.Inventory
local item = Inventory:FindFirstChild(itemName)
if item then
if item.Value > 0 then
item.Value = item.Value - 1
local itemClone = Items:FindFirstChild(itemName):Clone() --this line
itemClone.CFrame = player.Character.HumanoidRootPart.CFrame + player.Character.HumanoidRootPart.CFrame.LookVector * 6
itemClone.Parent = game.Workspace
return true
else
return false
end
end
end
I see. I recommend you read mine and @NoParameters’s reply. FestivePara has also written some sample code for checking that. First check if the item exist and if it does, then clone it. I recommend that you either use the debugger or put print statements between lines to debug the code (such as checking what itemName is and why it might not be in that location). This is what I usually do to debug.