How to detect items on the player's inventory?

Hello my fellas! I need help on my game that I did on my main account, that I tried to make a anti-cheat to don’t duplicate the player sword, but when you equips a sword, and enters the portal, you just get another sword, but if you don’t equip the sword, it kills you.

Can I fix that?

Code:

local db = false

script.Parent.Touched:Connect(function(obj)
	if obj.Parent:FindFirstChild("Humanoid") then
		if not db then
			db = true
			
			-- TELEPORT
			
			local spawns = game.Workspace["Grass Paradise"].Spawns:GetChildren()
			local random = math.random(1, #spawns)
			
			if random == 1 then
				obj.Parent:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace["Grass Paradise"].Spawns.Spawn1.CFrame
				
			elseif random == 2 then
				obj.Parent:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace["Grass Paradise"].Spawns.Spawn2.CFrame
				
			elseif random == 3 then
				obj.Parent:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace["Grass Paradise"].Spawns.Spawn3.CFrame
				
			elseif random == 4 then
				obj.Parent:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace["Grass Paradise"].Spawns.Spawn4.CFrame
				
			elseif random == 5 then
				obj.Parent:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace["Grass Paradise"].Spawns.Spawn5.CFrame
				
			elseif random == 6 then
				obj.Parent:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace["Grass Paradise"].Spawns.Spawn6.CFrame
			end
			
			-- GIVE SWORD
			
			task.wait(0.1)
			
			-- First, checking if player has sword.
			for i, v in game.Players:FindFirstChild(obj.Parent.Name).Backpack:GetChildren() do
				if game:GetService("CollectionService"):HasTag(v, "Sword") then
					obj.Parent.Humanoid.Health = 0
				end
			end

			local sword: Configuration = game.Players:FindFirstChild(obj.Parent.Name).Configuration
			local swordName = sword:GetAttribute("Sword")

			local cloned = game.ReplicatedStorage.Swords:FindFirstChild(swordName):Clone()
			cloned.Parent = game.Players:FindFirstChild(obj.Parent.Name).Backpack
			
			task.wait(1)
			db = false
		end
	end
end)

(Sorry if I typed something wrong, I’m not a american.)

1 Like

You can use :FindFirstChild() to check if either player.Backpack or Player.Character has the sword as one of their childs

Make the player unequip their item when entering a portal by using Humanoid:UnequipTools().

1 Like

I added obj.Parent:FindFirstChild("Humanoid"):UnequipTools() to the script, and it works, thank you @catfisheat2IsCool!

1 Like