Removing sword not working the best way

Well im developing a game about swordfighting, im building a practice zone in the lobby. The system is basically a part that gives it when touching it and a part that removes it. The problem is that when the player does a jump and exits the zone it doesnt remove the sword from him or even sometimes the script gives him two swords and when he exits the zone it just deletes one of the swords. This bug only works when the player exits the zone with a jump and i really dont know how to solve it.
Here is the script

    if v.Name == "ArenaLeave" then 
	     v.Touched:connect(function(Leave) 
			if Leave.Parent:FindFirstChild("Humanoid") then
				local plr = game.Players:GetPlayerFromCharacter(Leave.Parent) 
				local bp = plr.Backpack
				if bp:FindFirstChild("ClassicSword") ~= nil then 
					bp.ClassicSword:Remove() 
					end
				if Leave.Parent:FindFirstChild("LinkedSword") ~= nil then
					Leave.Parent.ClassicSword:Remove()
				end
			end 
		end) 
	end 
end 

That is specifically the script for removing the sword.

Touched events are notoriously buggy so I would recommend not using them. Instead, create a flat zone out of part(s) that you want to represent your zone. You can then raycast downwards from the character’s HumanoidRootPart with a whitelist of the folder that holds those zone parts.

4 Likes

Do you have any visual I can see to try it?

I see a few issues in your script you are only checking the backpack for classic sword one time and also only checking the character 1 time which I am guessing is Leave.Parent
Also you are using :Remove() this has been deprecated for a very long time its what we used to use back in the 07-12 days so you shouldn’t use that anymore instead use :Destroy()

below is your script fixed to loop through the backback and check for either sword and remove it and also through the character which is where the tool/sword maybe if it is equipped

if v.Name == "ArenaLeave" then 
	v.Touched:connect(function(Leave) 
		if Leave.Parent:FindFirstChild("Humanoid") then
			local Character = Leave.Parent  -- set a variable for the character..
			local plr = game.Players:GetPlayerFromCharacter(Character) 
			local bp = plr.Backpack
			for _, tool in ipairs(Character:GetChildren()) do   -- check through the character's children for either sword and remove if found
				if tool.Name == 'ClassicSword' or tool.Name == 'LinkedSword' then
					tool:Destroy()
				end
			end
			for _, tool in ipairs(bp:GetChildren()) do   -- go through all children in backpack if either has the name of the swords remove them
				if tool.Name == 'ClassicSword' or tool.Name == 'LinkedSword' then
					tool:Destroy()
				end
			end
		end 
	end) 
end
2 Likes

Alright,seems good enough. Ill try it and tell you if it works :slight_smile:

didnt work, this is what i got on the output

This is because you didnt close end at line 5th

it should be end) not end on line 5th

specifically at what part of the script?