How can I give and remove the sword from the player when they step in a box

I want to give and remove a sword from a player when they enter a box how can I do this?

here’s my code:

	local character = hit.Parent
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if character:FindFirstChild("Humanoid") then
		for i, v in pairs(game.ReplicatedStorage:GetDescendants()) do
			if v:IsA("Tool") and v.Name == "ClassicSword" then
				v.Parent = plr.Backpack
			else
				
			end
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local character = hit.Parent
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if character:FindFirstChild("Humanoid") then
		for i, v in pairs(plr.Backpack:GetDescendants()) do
			if v:IsA("Tool") and v.Name == "ClassicSword" then
				v.Parent = game.ReplicatedStorage
			else
				
			end
		end
		for i, v in pairs(character:GetDescendants()) do
			if v:IsA("Tool") and v.Name == "ClassicSword" then
				v.Parent = game.ReplicatedStorage
			else

			end
		end
	end
end)```

Hello,

First you need to check if whatever hit your part is actually a humanoid, before getting the plr otherwise you might get an error if something other than a player hits it.

You also want to clone the sword instead of doing that as it is much more efficient.

Then destroy it after the plr leaves the area.

Hope this helps!

The problem is the touch ended detects multiple times and the touched fires multiple times and I need it to not and yes I used a debounce

so you want the player have no sword when outr of the box and have sword inside of the box?

yes that’s what i am attempting to do I scripted it but the touch events keep firing and I have a debounce

Heres my updated code:

local debounce2 = false
local sword = nil

script.Parent.Touched:Connect(function(hit)
	if not debounce2 then
		debounce2 = true
	local hum = hit.Parent:FindFirstChild("Humanoid")
	
		if hum and not debounce then
			debounce = true
			local s = game.ReplicatedStorage.ClassicSword:Clone()
			s.Parent = hit.Parent
			sword = s
		end
		debounce2 = false
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if not debounce2 then
		debounce2 = true
	local hum = hit.Parent:FindFirstChild("Humanoid")
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if hum and debounce then
			debounce = false
				sword:Destroy()
		end
		debounce2 = false
	end
end)```

this is the problem:

https://imgur.com/a/x75b5wI

I do not see a variable named “debounce” anywhere, only a variable named “debounce2”.

You are missing a lot of code in that snippet. I don’t see where you declare the variable “sword” either. Are you sure you sent the entire code snippet?

ok uh i have a thread for this:
here u go
https://devforum.roblox.com/t/make-player-have-a-tool-when-enter-a-area-part/1138206/4

umm I have them clearly in there debounce2 is literally the first line in the code

I was not talking about that. I meant the variable “debounce” which you referenced but never defined. Glad you got it working though!

1 Like