How to break for loop

LOCAL SIDE:

function Equip(tool, toolName)
	if OwnedTools:FindFirstChild(toolName) then

		for i, v in pairs(Character:GetChildren()) do
			if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
				v:Destroy()
			else
				
			end
		end

		for i, v in pairs(LocalPlayer.Backpack:GetChildren()) do
			if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
				v:Destroy()
			else
				
			end
		end
		
		for i, v in pairs(EquipToolsEvents:GetChildren()) do
			if v:IsA("RemoteEvent") 
				and table.find(AllToolsNames, v.Name) 
				and table.find(AllToolsNames, toolName) then
				v:FireServer()
				print("Fired Server") 
				--^^^ This prints 6 times, (the amount of tools)
				-- How do I make it only fire the the RemoteEvent-
				-- That matched the same tool name to the RemoteEvent name?
			end
		end
		
	else
		NotOwnedFunction()
	end
end

SERVER SIDE:

function ServerEquip(player, tool)
	if player:FindFirstChild("OwnedTools") then
		local OwnedTools = player.OwnedTools

		if OwnedTools:FindFirstChild(tool) then
			local newTool = OwnedTools[tool]
			local clonedTool = newTool:Clone()
			clonedTool.Parent = player.Backpack
		else
			warn("Player doesn't own: "..tool.Name)
		end
	end
end

I’m trying to make an inventory system that only allows 1 tool in the player’s backpack.
But the issue I’m having, (in the local script, 3rd for loop) the print prints 6 times, and equips me with 6 tools.

How do I fix this?
Any help is appreciated!

Use the continue keyword instead of the break keyword in the 2 for loops in the local script.

1 Like

Could you show me an example of that?
I’m having trouble understanding what you mean.

Infact, you probably don’t need the else in those two for loops:

		for _, v in pairs(Character:GetChildren()) do
			if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
				v:Destroy()
			end
		end

		for _, v in pairs(LocalPlayer.Backpack:GetChildren()) do
			if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
				v:Destroy()
			end
		end

Anyways, I managed to read your problem wrong, sorry.
The fix should be to create an array before the for loop and check if the tool name is in there. If not, add it in, and then fire to the server.

		local loadedToolNames = []
		for _, v in pairs(EquipToolsEvents:GetChildren()) do
			if v:IsA("RemoteEvent") 
				and table.find(AllToolsNames, v.Name) 
				and table.find(AllToolsNames, toolName)
				and (not table.find(loadedToolNames, v.Name)) then
				table.insert(loadedToolNames, v.Name)
				v:FireServer()
				print("Fired Server") 
			end
		end
1 Like

I still end up with the same problem, instead of getting 1 tool, I get all 6.

1 Like

You need to do and v.Name == toolName

if v:IsA("RemoteEvent") and v.Name == toolName
				and table.find(AllToolsNames, v.Name) 
				and table.find(AllToolsNames, toolName) then
				v:FireServer()
				print("Fired Server") 
				--^^^ This prints 6 times, (the amount of tools)
				-- How do I make it only fire the the RemoteEvent-
				-- That matched the same tool name to the RemoteEvent name?
			end

because you have it doing i, v in pairs and so it is just doing it for all of the children. You need to check if it is the child you want and if it is then run the function. You don’t need to break the loop.

1 Like

yes, that seemed to work!! thank you!!

1 Like

You can simply use :FindFirstChild

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