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

Selecting from a table and finding in an object? [Answered]

Asked by 5 years ago
Edited 5 years ago

Go to the very bottom to see the fix.

Hi there. So I have a pickup system to pick up objects from the world a put in in a player's custom inventory. I am also trying to create a crafting system to go along with this. I haven't written anything really for the system yet because I am stuck on this problem this the script. You see, the inventory is just a model that I set up. This is for testing before I really create one. And the inventory has 3 sticks and 2 logs. The script takes the 2 sticks but doesn't take any logs. This is where I got stuck

Server Script: * By the way, inventory is referenced, it's just at the very top so I didn't copy it from the script.

01local craft = {"Log", "Stick", "Stick"}
02local items = {}
03local needed = #craft
04local took = 0
05 
06for _,v in pairs(inv:GetChildren()) do
07    for _,slot in next, craft do
08        if v.Name == slot then
09            table.insert(items, v)
10        end
11    end
12end
13 
14if #items >= needed then
15    print("Enough")
View all 29 lines...

Hey guys, future me here. I solved this problem by simply adding a table.remove() to the script and it fixed it all.

0
So what's the question/problem? DanzLua 2879 — 5y
0
The script takes the 2 sticks but doesn't take any logs. namespace25 594 — 5y
0
^ thats the problem namespace25 594 — 5y
0
Basically, the question is: how do I find 1 log in the model at the same time finding 2 sticks in a model and destroying them. And only 2 even if there's 20. namespace25 594 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
01sticks = workspace.Inventory:GetChildren()
02logs = workspace.Inventory:GetChildren()
03took = nil
04for i=1,#sticks do
05    if took == 0 then
06        sticks[i]:Destroy()
07        took = took + 1
08    elseif took < 2 then
09        sticks[i]:Destroy()
10        took = took + 1
11    elseif took == 2 then
12        #sticks = #sticks - took
13    end
14end
0
I don't know what inv is referenced in so yeah. AlphaWolf536791 28 — 5y
0
This answer is way to different from the original script and doesn't help at all. namespace25 594 — 5y
0
well, part of the script stops the loop doesnt it? AlphaWolf536791 28 — 5y
Ad

Answer this question