For loop not working

Hello! I’m trying to make a security scanner, that checks whether a player has weapons or not.

The for loop is meant to check whether the player has any weapons (tools) in their Character (has any weapons equipped at the time), but it doesn’t work. The characterToolCount value doesn’t get changed and the print doesn’t outprint.

local part = script.Parent
local statusText = part.Parent.Screen.SurfaceGui:WaitForChild("ImageLabel"):WaitForChild("Status")
local YESsound = part.Parent.Sound:WaitForChild("YES")
local NOsound = part.Parent.Sound:WaitForChild("NO")

local DEBOUNCE = false
local characterToolCount = 0

local function onPartTouched(otherPart)
	if DEBOUNCE == false then
		DEBOUNCE = true
		local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

		if humanoid then
			local who = game.Players:FindFirstChild(otherPart.Parent.Name)
			local tools = who.Backpack:GetChildren()
			local characterTools = otherPart.Parent:GetChildren()

			if tonumber(#tools) >= 1 then
				statusText.Text = "YES"
				statusText.TextColor3 = Color3.fromRGB(170, 0, 0)
			elseif tonumber(#tools) == 0 then
				statusText.Text = "NO"
				statusText.TextColor3 = Color3.fromRGB(85, 255, 0)
			end
			for i, v in pairs(characterTools) do
				if v.ClassName == "Tool" and v:FindFirstChild("Authenticate") then
					print("has some character weapons")
					characterToolCount += 1
					print("added tool count")
				end
				if characterToolCount >= 1 then
					YESsound:Play()
					print("has character weapons")
					wait(2)
					statusText.Text = ""
				elseif characterToolCount == 0 and tonumber(#tools) == 0 then
					print("doesnt have any weapons")
					NOsound:Play()
					wait(2)
					statusText.Text = ""
				elseif characterToolCount == 0 and tonumber(#tools) >= 1 then
					print("has tool weapons")
					YESsound:Play()
					wait(2)
					statusText.Text = ""
				end
				break
			end
		end
		
		characterToolCount = 0
		wait(1)
		DEBOUNCE = false
	end
end

part.Touched:Connect(onPartTouched)

You are breaking out of the for loop after the first iteration, so it is checking one object, and whether or not that object is a “Tool”, it will break out of the loop. Remove the break and it should work.

3 Likes

Also had to move the rest of the lines outside of this loop, however, it helped me. Thank you!

1 Like

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