How to fix this duplicated items?

Sorry for the low quality, I recorded it in the studio

When a player enters the pvp zone, the player should get a combat item, Upon the player leaving the area, the item should be removed from the player’s inventory. Unfortunately, the current implementation is partially functional, as equipping the item and exiting the area will result in the player retaining the item, leading to duplication issues. If anyone possesses the expertise to resolve this issue, your assistance would be greatly appreciated. Thank you.

local Part = script.Parent.Parent.Mat
local Tool = game.ReplicatedStorage.Fists

local givenTools = {Tool}

Part.Touched:Connect(function(hit)
	local character = hit.Parent
	if character and character:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(character)
		local backpack = player.Backpack
		if not backpack:FindFirstChild(Tool.Name) and not givenTools[player.UserId] then
			local newTool = Tool:Clone()
			newTool.Parent = backpack
			local tool = backpack:FindFirstChild(Tool.Name)
			player.CharacterRemoving:Connect(function()
				tool:Destroy()
			end)
			givenTools[player.UserId] = true
		end
	end
end)

Part.TouchEnded:Connect(function(hit2)
	local character = hit2.Parent
	if character and character:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(character)
		local backpack = player.Backpack
		local tool = backpack:FindFirstChild(Tool.Name)
		if backpack:FindFirstChild(Tool.Name) then
			tool:Destroy()
		end
		givenTools[player.UserId] = false
	end
end)
1 Like

This could be the problem, I’m not sure

Check if the player already has the tool:

if not character:FindFirstChild(tool.Name) and not backpack:FindFIrstChild(tool.Name) then

instead you check only the backpack, not the character it self, when the player equips the tool it gets moved from the backpack to the character.

Else your table is quite incorrect.

This should be part of the if statement to destroy the tool, also replace local tool = ... when destroying it with

local tool = backpack:FindFirstChild(Tool.Name) or character:FindFirstChild(Tool.Name)

if tool then
   tool:Destroy()
   -- instead put here givenTools[player.UserId] = false
end

This part will destroy the tool if it’s also equipped by the player, though you might have issues with stopping the animation so instead get the humanoid and force unequip instead.

2 Likes

Then, how the script will be able to duplicate and add items to the player’s backpack?

Check out my previous post, the issue is when you equip the tool it gets a new parent.

1 Like

Originally, my computer is ruined, so I can’t do anything

1 Like

I knew what the problem is, the problem is about hit, so if hit, the player can hit more than once

1 Like

Yes, The bug is half fixed because if a player equips an item and clicks it sometimes dupes and when a player uses an item (clicks) it removes and clones again

do you know how to fix that? is touching is not available

1 Like

No, but I do this every day, and he hits more than once


I mean it before ruined my computer

1 Like

local part = script.Parent
local Tool = game.ReplicatedStorage.Fists

function onTouch(hit)
local character = hit.Parent
if character:FindFirstChild(“Humanoid”) then
local player = game.Players:GetPlayerFromCharacter(character)
if player then
if part.Touched == true then
player.Backpack:AddItem(Tool)
else
player.Backpack:RemoveItem(Tool)
end
end
end
end

part.Touched:Connect(onTouch)
I just make a new one and it still adds and removes the item I do not know what to do