How would I make this detection script work?

So basically I’m making a system that detects the player’s team and if they have a certain tool in their backpack. And if they do, the alarm will go off until that player is killed. The problem is I’m not sure why it isn’t working, and there are no errors, and I’m not sure how to add the system that detects if the player is killed.

Here’s the script:

local alarmsound = script.Parent.Parent.AlarmSound
local part = script.Parent

local weppons = {
	["[SCP] Card-Omni"] = true,
	["Gun1"] = true,
	["Gun2"] = true,
	["Gun3"] = true,
	["Gun4"] = false
}


function triggerAlarm()
	alarmsound:Play()
end

local function onPartTouched(hit)
	local IsInBackpack = false
	for index, v in pairs(weppons) do
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player.Backpack:FindFirstChild(index) then
			if player.Team.Name == "Class-D" then
				IsInBackpack = true
			end
		end
	end

	if IsInBackpack then
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Really red")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Really red")
		triggerAlarm()
	else
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lime green")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lime green")
		wait(1)
		script.Parent.Parent.Light1.BrickColor = BrickColor.new("Lily white")
		script.Parent.Parent.Light2.brickColor = BrickColor.new("Lily white")
	end
end
1 Like

The player might be holding the tool. In that case, the tool is located in the player’s character.

Yes, however even when the tool isn’t being held (still in the backpack) it still does not work.

You forgot to add the event when the part is touched, add this in the end of the script.

part.Touched:Connect(onPartTouched)

Well, to find out if a player is in a team you can do this:

if player.Team.Name == "team Name Here" then
    -- do the thing
end

And to check if a tool is in their backpack:

if player.Backpack:FindFirstChild("GunNameHere") then
   -- do the thing
end

Sorry for any mistakes, I’m on mobile.