How would I check if a player has more than one of the same item in their backpack?

I’ve tried searching for this but I’m not sure how to figure this out. Help

My current code:

local rs = game.ReplicatedStorage

script.Parent.Touched:Connect(function(Hit)
	if Hit then
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			local collect = rs.FGTeeV:Clone()
			collect.Parent = Player.Backpack
			script.Parent:Destroy()
		else
			script.Parent:Destroy()
		end
	end
end)

Sorry if this is already a topic lol.

you could compare the Instance.name of the two items.

A server-side Script example:

local item1 = game.ReplicatedStorage.ItemOne
--'player' is the player Instance
local backpack = player.Backpack
if not backpack:FindFirstChild(item1.Name) then
    print("Not in backpack")
    --code to put it in
else
    print("In backpack")
end

Bare in mind this is when adding a tool to the backpack.

I’m confused by this. Wouldn’t the player Instance be LocalPlayer? If so, wouldn’t that not work because this is a normal script?

sorry if im being dumb lol. im not the best scripter and ive spent like 2 hours trying to figure this out and im really tired

I assumed it was a server-side script. Sorry if it isn’t. LocalPlayer only works in a LocalScript.

So, you might have:

local player = game.Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")

Then, continue the comparison from there.
Make sure to use WaitForChild() when referencing a child of an instance, so that the client has time to register it.

However, remember anything edited client-side will only change for that player, and will not be registered server-side. You can use a RemoteEvent to communicate between client and server.

No, it is a server-side script. Sorry for confusing you lol. I’m very tired but I don’t also understand the server-side script example you sent.

The server-side example I sent uses the player’s Player object, whether it is obtained from a RemoteEvent, a part being touched, or something else. The Backpack is where the player’s unequipped tools are stored. The script I sent is to be used when you are trying to put an item into the Backpack. All it does is check to see if a tool of the same name already exists in the Backpack, which essentially is checking for the tool already in there. Just make sure no other tools have the same name.

The FindFirstChild() returns true if it is in there, and nil if it is not. Putting a not in front of it is basically saying ‘if it isn’t in there already’.

Figured it out. Thanks! characterlimit