I've tried to make a scanner script that scans for contraband when the player touches a part and if found any it would change the light colour. I've managed to do some scripting but I still get an error saying "Attempt to index nil with 'Backpack'. The script changes the colour of the light to read once and then it gives this error. Also, the script won't change the light's colour to green even if the player has no contraband.
local contraband = { "AK74M", "USP" } local players = game:GetService("Players") script.Parent.Touched:Connect(function(hit) local player = players:GetPlayerFromCharacter(hit.Parent) for i,v in pairs(player.Backpack:GetChildren()) do if table.find(contraband,v.Name) then script.Parent.Parent.Light.BrickColor = BrickColor.new("Maroon") elseif not table.find(contraband,v.Name) then script.Parent.Parent.Light.BrickColor = BrickColor.new("Bright green") end end end)
Try this and check if it works.
local contraband = { "AK74M", "USP" } local players = game:GetService("Players") script.Parent.Touched:Connect(function(hit) for i,v in pairs(player.Backpack:GetChildren()) do if table.find(contraband,v.Name) then script.Parent.Parent.Light.BrickColor = BrickColor.new("Maroon") elseif not table.find(contraband,v.Name) then script.Parent.Parent.Light.BrickColor = BrickColor.new("Bright green") end end end)
The problem was somehow in the part itself. When I walked through it, it returned this error but when I walked on it, it worked completely fine.