Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Inventory system not equipping item?

Hello,

i've recently been working on my first game and created an inventory system in it. I have one major problem though and its that it wont take a tool from the inventory and place it in your backpack.

local player = game.Players.LocalPlayer
local playerInv = game.Players.LocalPlayer:WaitForChild("SpellList")
local backpack = player.Backpack:GetChildren()
local itemName = script.Parent.Parent.ItemName
local itemToCheck = player.SpellList:FindFirstChild(itemName.Value)
if itemToCheck then
	local itemToEquip = player.SpellList:WaitForChild(itemName.Value)
	local itemInBP = backpack:FindFirstChild(itemName.Value)
	if #backpack < 2 and not itemInBP then
	local equipToBP = itemToEquip:Clone()
	equipToBP.Parent = player.Backpack
	elseif #backpack >= 2 then
		print("Unequip an item!")
	elseif itemInBP then
		itemInBP:Destroy()
	end
end 

inside of the gui that i created for the items there is a string value that gets changed to the tool's name so that i can use FindFirstChild() to search for whatever the tools name is in their inventory folder named "SpellList". The console isn't spitting out any errors and when i click the equip button nothing happens. the itemInBP and #backpack are to check if the player has more than 2 items equipped and if they already have the item equipped.

2 answers

I think you just wasn't updating after they clicked it, try:

local player = game.Players.LocalPlayer
local playerInv = game.Players.LocalPlayer:WaitForChild("SpellList")
local backpack = player.Backpack:GetChildren()
local itemName = script.Parent.Parent.ItemName
local itemToCheck = player.SpellList:FindFirstChild(itemName.Value)
local EquipRequest = player.SpellList:WaitForChild(itemName)

EquipRequest.Changed:Connect(function()
	if itemToCheck then
	    local itemToEquip = player.SpellList:WaitForChild(itemName.Value)
	    local itemInBP = backpack:FindFirstChild(itemName.Value)
	    if #backpack < 2 and not itemInBP then
	    local equipToBP = itemToEquip:Clone()
	    equipToBP.Parent = player.Backpack
	    elseif #backpack >= 2 then
	        print("Unequip an item!")
	    elseif itemInBP then
	        itemInBP:Destroy()
	    end
	end
end)

Let me know if this works ~nevan

2 comments

0

thanks for the answer, i actually just forgot to put script.Parent.Mouse1ButtonClick:Connect(function()

0

Alright, glad you fixed it

So im a big dumb. I never put it to activate the script if the button is pressed

Its fully working now.

This discussion is preserved as it was. New answers, comments, and votes are closed.