Issues with metal detector script

I made a metal detector using ZonePlus, and it works but there’s a couple of issues.

First of all, both the sounds for when it detects you don’t have any contraband and when it detects contraband play at the same time, only if it detects contraband.
Otherwise it would only play the sound that means you are clear.

Second, if your last equipped item was not contraband, then the light would flash green, even if you do have contraband in your inventory.

I’m not really sure on how to do this properly, so I’m asking for help.

The script:

local rs = game:GetService("ReplicatedStorage")
local db = game:GetService("Debris")
local z = require(rs.Modules:WaitForChild("Zone"))

for i,container in pairs(workspace.MetalDetectors:GetChildren()) do
	for i, detector in pairs(container:GetDescendants()) do
		if detector.Name == "DetectionPart" then

			local zone = z.new(detector)
			zone:relocate()

			local debounce = false


			zone.playerEntered:Connect(function(plr)
				if not debounce then
					debounce = true
					for i,v in pairs(plr.Character:GetChildren()) do
						if v:IsA("Tool") then
							if v:FindFirstChild("Config") then
								local t = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame.Template:Clone()
								t.UIStroke.Color = Color3.fromRGB(255, 0, 0)
								t.TextColor3 = Color3.fromRGB(255, 0, 0)
								t.Text = v.Name
								t.Visible = true
								t.Parent = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame

								container.MetalDetector.LightPart.Alert:Play()
								container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = plr.Name

								if container.MetalDetector:FindFirstChild("LightPart") then
									coroutine.wrap(function()
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 0, 0)
										wait(1)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 0)
										wait(10)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 255)
										container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = "N/A"
										t:Destroy()
										debounce = false
									end)()
								end
							else
								local t = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame.Template:Clone()
								t.UIStroke.Color = Color3.fromRGB(222, 222, 222)
								t.TextColor3 = Color3.fromRGB(222, 222, 222)
								t.Text = v.Name
								t.Visible = true
								t.Parent = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame

								container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = plr.Name

								container.MetalDetector.LightPart.Clear:Play()
								if container.MetalDetector:FindFirstChild("LightPart") then
									coroutine.wrap(function()
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(85, 255, 0)
										wait(1)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 0)
										wait(10)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 255)
										container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = "N/A"
										t:Destroy()
										debounce = false
									end)()
								end
							end
						end
					end
					for i,v in pairs(plr.Backpack:GetChildren()) do
						if v:IsA("Tool") then
							if v:FindFirstChild("Config") then
								local t = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame.Template:Clone()
								t.UIStroke.Color = Color3.fromRGB(255, 0, 0)
								t.TextColor3 = Color3.fromRGB(255, 0, 0)
								t.Text = v.Name
								t.Visible = true
								t.Parent = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame

								container.MetalDetector.LightPart.Alert:Play()
								container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = plr.Name

								if container.MetalDetector:FindFirstChild("LightPart") then
									coroutine.wrap(function()
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 0, 0)
										wait(1)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 0)
										wait(10)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 255)
										container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = "N/A"
										t:Destroy()
										debounce = false
									end)()
								end
							else
								local t = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame.Template:Clone()
								t.UIStroke.Color = Color3.fromRGB(222, 222, 222)
								t.TextColor3 = Color3.fromRGB(222, 222, 222)
								t.Text = v.Name
								t.Visible = true
								t.Parent = container.Screen.Screen.ScreenUI.Main.DetectionFrame.Frame

								container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = plr.Name

								container.MetalDetector.LightPart.Clear:Play()
								if container.MetalDetector:FindFirstChild("LightPart") then
									coroutine.wrap(function()
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(85, 255, 0)
										wait(1)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 0)
										wait(10)
										container.MetalDetector:FindFirstChild("LightPart").Color = Color3.fromRGB(255, 255, 255)
										container.Screen.Screen.ScreenUI.Main.DetectionFrame.PlayerName.Text = "N/A"
										t:Destroy()
										debounce = false
									end)()
								end
							end
						end
					end
				end
			end)
		end
	end
end
1 Like

This issue stems from the fact that you are checking the player’s character and backpack for tools but not communicating between the checks. So if a player has contraband in their backpack, not equipped, it is playing both sounds because their character was clear, but their backpack wasn’t. Same issue with the lights.

I would create a new function “check” and move everything relating to checking the character & backpack under that. Then you can call the check function and based on whatever you return “contraband” or “clear”, you can accurately change the light color/play the correct sound.

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