Sword won't disappear from inventory unless equipped

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == true then
			local plr = game.Players[hit.Parent.Name]
			hit.Parent.Sword:Destroy()
			plr.Backpack.Sword:Destroy()
			db = false
		end
	end
end)

This script is to make it so when a player leaves the fight zone it unequips the sword, and then removes it from their inventory. It errors ‘Sword is not a valid member of vf9r.Backpack’

What am I doing wrong?

I mean local plr = game.Players.LocalPlayer

Same error still, that is the exact same thing except its waiting until it exists.

This is in a server script, not a local script.

You aren’t implementing another sanity check if the Player’s Character or Player’s Backpack has the tool, to fix this just do:

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == true then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
            if plr then 
                if hit.Parent:FindFirstChild("Sword") then
		        	hit.Parent.Sword:Destroy()
                else
                    plr.Backpack.Sword:Destroy()
                end
            end
			db = false
		end
	end
end)

When a tool is equipped, it is moved from the backpack to the character, so you have to check both locations.

Try this:

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == true then
			local plr = game.Players[hit.Parent.Name]
            local hitParentSword = hit.Parent:FindFirstChild("Sword")
            local backpackSword = plr.Backpack:FindFirstChild("Sword")
            if hitParentSword then
	    		hitParentSword:Destroy()
            elseif backpackSword then
    			backpackSword:Destroy()
            end
			db = false
		end
	end
end)

Now whenever I try to equip it, it won’t equip it just automatically removes it.
https://gyazo.com/e2ba2bb114016f08beaf9342946ef641

Top of script (if needed)

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if db == false then
			local plr = game.Players[hit.Parent.Name]
			if not plr.Backpack:FindFirstChild("Sword") then
				game.ServerStorage.Sword:Clone().Parent = plr.Backpack
				db = true
			else
				return
			end
		end
	end
end)

I wouldn’t recommend using TouchEnded in this Instance then, it serves as a very unprecise & wonky result

A better alternate would be to probably use Region3

I don’t know how to use Region3, and the documentation on it is very unclear on how to use it in this instance.

Well, here’s what I would do then for this situation:

  • Create 2 invisible parts outside the fight zone, but touching each other & they’re both anchored

  • Have the part from the far outside detect for any players that might have a sword, then remove it

  • Have the part from the close outside detect for any players that don’t have a sword, then give them one

Probably something relevant to this:

--Part that will give the Tool
local Part = script.Parent

Part.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player.Backpack:FindFirstChild("Sword") == nil and Player.Character:FindFirstChild("Backpack") == nil then
            local SwordClone = game.ServerStorage.Sword:Clone()
            SwordClone.Parent = Player.Backpack
        end
    end
end)
--Part that will destroy the sword
local Part = script.Parent

Part.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player.Backpack:FindFirstChild("Sword") then
            Player.Backpack.Sword:Destroy()
        elseif Player.Character:FindFirstChild("Sword") then
            Player.Character.Sword:Destroy()
        end

    end
end)