Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Inventory system not equipping item?

Asked by 4 years ago

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

Log in to vote
0
Answered by
I_Nev 200 Moderation Voter
4 years ago

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

0
thanks for the answer, i actually just forgot to put script.Parent.Mouse1ButtonClick:Connect(function() Idoskii 4 — 4y
0
Alright, glad you fixed it I_Nev 200 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Its fully working now.

Answer this question