Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

4 comments

0

So what's the question/problem?

0

The script takes the 2 sticks but doesn't take any logs.

0

^ thats the problem

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.

1 answer

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

3 comments

0

I don't know what inv is referenced in so yeah.

0

This answer is way to different from the original script and doesn't help at all.

0

well, part of the script stops the loop doesnt it?

This discussion is preserved as it was. New answers, comments, and votes are closed.