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\--
01 | local player = game.Players.LocalPlayer |
02 | local template = script.Parent.Parent.Template |
03 |
04 | wait(player.CharacterAdded) |
05 |
06 | function addItems() |
07 | local invItems = player.Inventory:GetChildren() |
08 | if invItems = = "TestItem" then |
09 | local clonedTemp = template:Clone() |
10 | clonedTemp.Parent = script.Parent |
11 | clonedTemp.InfoValues.Gives.Value = 1 |
12 | clonedTemp.InfoValues.Name.Value = "TestItem" |
13 | clonedTemp.Visible = true |
14 | end |
15 | end |
16 |
17 | addItems() |
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
.
01 | local player = game.Players.LocalPlayer |
02 | local template = script.Parent.Parent.Template |
03 |
04 | wait(player.CharacterAdded) |
05 |
06 | function addItems() |
07 | local invItems = player.Inventory:GetChildren() |
08 | for _,v in pairs (invItems) do |
09 | if v.Name = = "TestItem" then |
10 | local clonedTemp = template:Clone() |
11 | clonedTemp.Parent = script.Parent |
12 | clonedTemp.InfoValues.Gives.Value = 1 |
13 | clonedTemp.InfoValues.Name.Value = "TestItem" |
14 | clonedTemp.Visible = true |
15 | end |
16 | end |
17 | end |
18 |
19 | addItems() |
Miniller did actually have the right idea. He simply didn't elaborate on it enough.
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:
1 | player.CharacterAdded:Connect( function () |
2 | --put all your code (except the line01) there! |
3 | end ) |
For the other problems I said, the backpack and the table thing, use
1 | for i = 1 , #player.Backpack:GetChildren() do |
2 | print (player.Backpack:GetChildren() [ i ] ) |
3 | end |
4 | --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.