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

(Solved!) Not Adding Items To Inventory? No Output Errors!

Asked by 5 years ago
Edited 5 years ago

The script isn't adding the items to the inventory as asked and there's no output errors!

There's probably something way wrong with this script but here it is!

--//ITS A LOCAL SCRIPT\--

local player = game.Players.LocalPlayer
local template = script.Parent.Parent.Template

wait(player.CharacterAdded)

function addItems()
    local invItems = player.Inventory:GetChildren()
    if invItems == "TestItem" then
        local clonedTemp = template:Clone()
        clonedTemp.Parent = script.Parent
        clonedTemp.InfoValues.Gives.Value = 1
        clonedTemp.InfoValues.Name.Value = "TestItem"
        clonedTemp.Visible = true
    end
end

addItems()

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Despite incapaxx's comment (I literally have no idea what he's talking about; he's probably high again), :GetChildren() returns a table. This means that invItems will never be equal to TestItem. If you printed it, you'd get a TableId outputted. I'm assuming Inventory is a folder or any data container that you've created, and you want to scan inside it for the so called TestItem. To accomplish this, you'll have to loop through the table returned by :GetChildren(), i.e. invItems.

local player = game.Players.LocalPlayer
local template = script.Parent.Parent.Template

wait(player.CharacterAdded)

function addItems()
    local invItems = player.Inventory:GetChildren()
    for _,v in pairs(invItems) do
       if v.Name == "TestItem" then
           local clonedTemp = template:Clone()
           clonedTemp.Parent = script.Parent
           clonedTemp.InfoValues.Gives.Value = 1
           clonedTemp.InfoValues.Name.Value = "TestItem"
           clonedTemp.Visible = true
       end
    end
end

addItems()

Miniller did actually have the right idea. He simply didn't elaborate on it enough.


Accept and upvote if this helps!

0
It's not working iiDev_Hunt3r 64 — 5y
0
is there a child in Inventory called TestItem and are there any errors? Gey4Jesus69 2705 — 5y
0
changed line 9, my bad. try now unless there are different errors Gey4Jesus69 2705 — 5y
0
i'm just better than u and i'm not high User#24403 69 — 5y
1
It works now iiDev_Hunt3r 64 — 5y
Ad
Log in to vote
1
Answered by
Miniller 562 Moderation Voter
5 years ago
Edited 5 years ago

Problem 01:Line 08 is not working, because player.Inventory:GetChildren() returns a table. Problem 02:It's Backpack, not Inventory, Problem 03: I don't think wait(player.CharacterAdded) is working, because there you need to write a number... For that, you need to add an event like this:

player.CharacterAdded:Connect(function()
    --put all your code (except the line01) there!
end)

For the other problems I said, the backpack and the table thing, use

for i = 1, #player.Backpack:GetChildren() do
    print(player.Backpack:GetChildren()[i])
end
--This code will print all tools in backpack. 

instead of what you used. Also, where is this script at? I need to know that! Tell me where is the script, and I can help you to re-script that.

0
invItems will only hold the first return value of unpack User#24403 69 — 5y
0
Then how to detect all items? Miniller 562 — 5y
0
incapaz is rarted Gey4Jesus69 2705 — 5y

Answer this question