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.

local craft = {"Log", "Stick", "Stick"}
local items = {}
local needed = #craft
local took = 0

for _,v in pairs(inv:GetChildren()) do
    for _,slot in next, craft do
        if v.Name == slot then
            table.insert(items, v)
        end
    end
end

if #items >= needed then
    print("Enough")

    spawn(function()
        while took < needed do
            for _,v in pairs(inv:GetChildren()) do
                for _,slot in next, craft do
                    if v.Name == slot and took < needed then
                        v:Destroy()
                        took = took + 1
                    end
                end
            end
        end
    end)
end

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
sticks = workspace.Inventory:GetChildren()
logs = workspace.Inventory:GetChildren()
took = nil
for i=1,#sticks do
    if took == 0 then
        sticks[i]:Destroy()
        took = took + 1
    elseif took < 2 then
        sticks[i]:Destroy()
        took = took + 1
    elseif took == 2 then
        #sticks = #sticks - took
    end
end
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