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’
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)
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)
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)