Ilerate through backpack and character

I’m trying to loop through the player backpack and character to check for a certain instance (tool), how do i do that? I don’t wanna use two simultaneous loops.

For the backpack you can use a simple for loop, for the character you can use FindFirstChildOfClass("Tool"), if it gives nil there’s no tool in the character, otherwise it gives the tool

here is a example:

local player = game.Players.LocalPlayer
local backpack = player.Backpack
local character = player.Character

for _, child in pairs(backpack:GetChildren()) do
  if child.Name == "Tool" then
    -- perform action if tool is found in backpack
    break
  end
end

if not child then
  for _, child in pairs(character:GetChildren()) do
    if child.Name == "Tool" then
      -- perform action if tool is found in character
      break
    end
  end
end

This code loops through the backpack and character of the local player to check for a child instance with the name “Tool”. If it’s found in the backpack, the loop is broken and the action is performed. If not found, it then loops through the character and performs the action if found.

I’m still confused, since i have a max slots thing system where only one tool is used for the max slots
like this:

function PlayerModule:GetTools(Player)
	local nums = #Player.Backpack:GetChildren() -- cant do that in a nomral loop apparently
	for _,v in Player.Backpack:GetChildren() do
		if v:FindFirstChild("StateGive") then -- this is the tool that should be blacklisted in maxslots.
			if nums <= Player.MaxSlots then
				-- Give tool
			end
		end
	end
end

But it bugs out if the player has tools equipped.

What are you trying to do exactly if I may ask?

I have a backpack max slots system, but ONLY one tool with StateGive child gets blacklisted while other tools that does not have the child are allowed to bypass the max slots.

If I understood well, tools with a StateGive child aren’t included in your max slots system but tools that don’t are counted for the max slot thing to prevent giving a tool?

Quite the opposite, tools with StateGive are counted for the maxslots (Don’t clone further tools with the same child) while tools that DONT have it are NOT counted for the maxslots therefor bypassing it. (Clone any tools that don’t have it)

In that case, I think what you can do, if I understood well, have a variable that stores how many tools with StateGive you have, loop through backpack and increment the variable for each StateGive child tool you find, then for the character, use the function I mentioned and if it found a tool, check if it has the child, if it does increment the variable. Once you’re done with that, compare that variable with the MaxSlots and if it’s lower, give the tool

Hopefully I understood what you’re trying to do, forgive me if I didn’t

Tried to do my best from what i understood but it still gave me the same tool regardless of my MaxSlots (it was only 1, meaning i should only get one StateGive tool)

function PlayerModule:ReturnTools(Player)
	local Tools = Player.Backpack:GetChildren()
	for _,v in Tools do 
		if v:FindFirstChild("StateGive") then
			if #Tools <= Player:FindFirstChild("Powerups"):FindFirstChild("MaxSlots").Value then
				return true  -- should not clone but it does
			end
		end
	end
end

function PlayerModule:GetItems(Player:Player?)
	local Tool = Player.Backpack:FindFirstChildWhichIsA("Tool")
	local Character = Player.Character
	if Character then
		local Tool = Character:FindFirstChildWhichIsA("Tool")
		if Tool then
			local Stone = Tool:FindFirstChild("StateGive")
			if Stone then return true end
		end
	end
end

I think you have to do something like this

certainTools = 0

for _, tool in ipairs(backpack) do
	if not tool:FindFirstChild("StateGive") then
		continue
	end
	
	certainTools += 1
end

characterTool = character:FindFirstChildOfClass("Tool")
hasStateGive = characterTool and characterTool:FindFirstChild("StateGive")
if hasStateGive then
	certainTools += 1
end

if certainTools <= Player.MaxSlots then
	-- Clone tool
end

Of course not exact since it’s somewhat pseudo, thing that I might be still confused about is what you do with the StateGive tools since you mention cloning

From what i’ve seen in the snippet, Should i just use a numbervalue then add up the value everytime the tool with StateGive is added through the player backpack? after that we compare with a loop?

If you think that would be a better solution for you, then you can try that, I tried to do it based on how I understood since I wasn’t sure on the purpose of the cloning, since I’m assuming you’re getting how many StateGive tools the player has to not give them another if they’re above their maxslots

Well it sorta works, i can detect the max items player has and compare it with the max slots. but the value does not add up? (I had to manually change the value everytime i gave myself the tool)

function PlayerModule:Test(Player:Player?)
	if Player.test.Value ~= Player:FindFirstChild("Powerups"):FindFirstChild("MaxSlots").Value then
		print("give this person a tool") 
	else
		print("DO NOT")
	end
end
--
local Slots = Instance.new("IntValue")
	Slots.Name = "test"
	Slots.Parent = Player
Player.Backpack.ChildAdded:Connect(function(Child)
	if Child:FindFirstChild("StateGive") then
		Slots.Value += 1
	end
 end)

Maybe try doing how I said with a variable in the part where you check how many StateGive tools they have? (Looping through backpack and checking if the character has a tool)

This works! Thank you, this is like the 4th time i’ve rewrote this system due to how complex it is. I only have to figure out what to do with the tool being equipped.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.