Hello. I am trying to code a metal detector. So if a player has a certain tool in their backpack, then a display part will turn red and play a sound… The problem is the display won’t turn red even when the certain part is in my backpack…
The code is below…
local detector = script.Parent
local Display = workspace.ToolDetect.Model2.Display
local debounce = false
local tooldetected = false
local beep = script.Parent.Sound
detector.Touched:Connect(function(hit)
if debounce == false then
debounce = true
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
if player ~= nil then
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Flash Light") or v:IsA("Other") then
tooldetected = true
end
end
if tooldetected == true then
Display.BrickColor = BrickColor.new("Really red")
beep:play()
tooldetected = false
else
Display.BrickColor = BrickColor.new("Lime green")
tooldetected = false
end
wait(1.5)
Display.BrickColor = BrickColor.new("Really Black")
end
debounce = false
end
end)
Here is a video also: