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

trying to make a invatory script and i ran into a problem at the for loop part?

ok so here is my code

game.Players.PlayerAdded:Connect(function(p)
	
	slots = Instance.new('Folder', p)
	slots.Name = 'slots'
	
	slot1 = Instance.new('Folder', slots)
	slot1.Name = 'slot1'
	
	slot2 = Instance.new('Folder', slots)
	slot2.Name = 'slot2'
	
	slot3 = Instance.new('Folder', slots)
	slot3.Name = 'slot3'
	
	slot4 = Instance.new('Folder', slots)
	slot4.Name = 'slot4'
	
	weapons = Instance.new('Folder', p)
	weapons.Name = 'weapons'
	
	a = game.ReplicatedStorage.tool:Clone()
	a.Parent = weapons
	
	b = game.ReplicatedStorage.tool:Clone()
	b.Parent = weapons
	
	for i,o in ipairs(p.weapons:GetChildren()) do
		for v,b in ipairs(p.slots:GetChildren()) do 
			if b:FindFirstChild('tool') == nil then
				print('it broke!')
				break
			else
				o.Parent = b
			end
			
		end
	end
	
	

so what this is (Suppose) to do is make a weapons folder, a slots folder and inside the slots folder 4 other folders and then the game will grab weapons from the replicated storage andn put it in the weapons folder then this is were im lost im trying to get all of the weapons inside the weapons folder and put it in a slot individually if that makes since so if a slot has a weapons inside of it ('tool') then it would move on to the next slot and put it there can somone please guide me on how to do this?

1 answer

✓ Accepted answer

You have your code backwards. You are breaking the if statement if there is nothing named 'tool' in b(the slot), when you should be adding the tool to b.

So, it should be as follows:

for i,o in ipairs(p.weapons:GetChildren()) do
        for v,b in ipairs(p.slots:GetChildren()) do 
            if b:FindFirstChild('tool') ~= nil then --Check to see if 'tool' is a child of b and if so
		print('tool found in '..b.Name..'. Moving on')-- do this
            else
                o.Parent = b --otherwise add the tool to b(since no other object named 'tool' is a child of b)
            end
        end
    end

A tip- If you plan on checking whether or not any object is in the inventory, instead of checking specifically for 'tool', then I recommend putting a value on items indicating whether or not they are an actual inventory item.

1 comment

0

it worked thanks man!

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