Sword Fighting Area; tool script not working

local IsTouching = false

script.Parent.Touched:connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if hum and not IsTouching then
		IsTouching = true
		if plr.Backpack:FindFirstChild("Sword") == nil then
				game.ServerStorage.Sword:clone().Parent = plr.Backpack
			end
		end
end)

script.Parent.TouchEnded:connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if hum and IsTouching then
		IsTouching = false
		for _,v in pairs(plr.Character:GetChildren()) do
			if v:IsA("Tool") and v.Name == "Sword" then
				v:Destroy()
			end
		end
	end
end)

Above is a server-side script located inside the part that the player has to be standing on in order to have a Sword in their inventory.

The goal of this script is to make it so they can only sword fight when they are within the arena limits. However the issue I’m having with it right now is it won’t let you equip it. It continues to remove your sword and assign you a new one. Making it impossible to equip a sword for longer then a second.

I’m very rusty at scripting, where am I going wrong? D:

The answer is pretty obvious.

The event you connected this function to is TouchedEnded, which will fire when the player stops touching the sword dispenser. And when the function is called, it searches for any sword in the player’s inventory and just straight up deletes them.

No idea how you missed that.

I’ve missed huge bugs in my code before, It’s not really that rare

Let me explain further. They are standing on top of the “sword giver.” So I am not stepping off of the platform and it is still removing the sword.

If they’re standing on top of it, if the player’s feet step off for example when walking it will fire the touchended event

Then I’m not sure. I’m basing my conclusion on the code snippet you provided us. TouchEnded is an event that fires when the player walks away from the sword giver.
You can show us more of the script so we can get a better understanding of it

Just think I’m going to switch this over to a magnitude check - and if they’re too far from the area it will just remove your sword.

I had a horrible approach at this, not sure what I was trying to accomplish with this method.

1 Like

You could try to use region3 or magnitude instead of .Touched, In my testing .Touched isn’t very reliable.

1 Like

Or you could alternatively encase the entire arena in an invisible hitbox and TouchEnded would work fine like that

so something like this might work better

local Register = {}
local approxPartRadius = (part.Size.X + part.Size.Y + part.Size.Z)  / 2

local function OnTouch(hit)
	local root = hit.Parent:FindFirstChild("HumanoidRootPart")
	if not root then return end --non-humanoid trigger

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end --NPC trigger

	if Register[player] then return end --player is already registered
	Register[player] = true


	--Do some stuff here
	
	
	--keep checking character position, and remove from register if player leaves
	--also keep checking Parent to make sure character is still in the game
	while hit.Parent and (root.Position - part.Position).Magnitude < approxPartRadius do
		wait(.1)
	end

	--this section activates on player leaving the area
	Register[player] = nil
end

part.Touched:Connect(OnTouch)

I forgot where I found it but it is a post somewhere on here

pssst, Region3 functions are deprecated/will be deprecated in favor of those cool new spatial query functions

1 Like

Thanks. Didn’t know this! Time to go learns a cool new thing!
(I’ve only ever used region3 once so I don’t keep up with it lol)