Give/Remove tool script not working

  1. What do you want to achieve?
    I’m trying to make a simple pvp area and remove the tool from the player when they leave and give them a tool when they enter.

  2. What is the issue?
    The script keeps on giving the player the tool even though it isn’t supposed to.

  3. What solutions have you tried so far?
    I’ve tried messing around with my script.

local Tool = game:GetService("ServerStorage"):WaitForChild("Tools"):WaitForChild("ClassicSword")
local box = script.Parent.HitBox

box.Touched:Connect(function(hit)	
	if not hit.Parent:FindFirstChild("Humanoid") or hit.Name ~= "HumanoidRootPart" then return end
	
	local hum = hit.Parent:FindFirstChild("Humanoid")
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hum.Parent)

	local hasTool = hum.Parent:FindFirstChild("ClassicSword") or plr.Backpack:FindFirstChild("ClassicSword")
	if hasTool then return end
	
	local tool = Tool:Clone()
	tool.Parent = hum.Parent
end)

box.ToucheEnded:Connect(function(hit)
	if not hit.Parent:FindFirstChild("Humanoid") or hit.Name ~= "HumanoidRootPart" then return end
	
	local hum = hit.Parent:FindFirstChild("Humanoid")
	
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hum.Parent)
	local tool = hum.Parent:FindFirstChild("ClassicSword") or plr.Backpack:FindFirstChild("ClassicSword")
	
	if not tool then return end
	
	tool:Destroy()
end)

I realized I used .Touched for both events, which I fixed but I still spam equip the swords.

add a debounce and see if its spamming the swords or not

1 Like

Do you get any errors in the output?

1 Like

Then there would be a debounce for the entire server.

The entire output is empty, no messages, errors or warnings.

Ok, give me a few minutes and I will do some testing.

1 Like

For some reason I can’t even replicate it removing, it only gives me the tool when I try to replicate this.

Would it be possible for u to send me modal which includes the code and the modals (do not need to be the exact modals, they can be like just bricks).

1 Like

(rbxl file removed)
Sorry for late reply, I was away.
This is the entire game I’m making, but anyways you want to walk into the sand pit thing to activate the event.

Ok, so the issue is the way you have the code is when you walk or move it will basically end the touch but then also start it again.

In general .touched is not the best for what ur doing. Your best bet would to use something like raycasting or if u want to keep the touched system have a part or parts which surround the fighting area so if the user touches the box around and has a sword it will be removed.

1 Like

I’ll probably go with the 2nd option as I’m not experienced with raycasting but thank you anyways.