Theres a bug in this script (sword)

the sword will damage people even not Activated

here is the script

local tool = script.Parent
local Blade = tool.Blade
local Handle = tool:WaitForChild("Handle")
local SwingDebounce = false
local DamageDebounce = false

local Damage = script.Parent.damage

local CanDamage = tool.Parent.Parent:WaitForChild("CanDamage")

local function TagHumanoid(Humanoid,Killer)
	if Humanoid and Killer then
		local Tag = Instance.new("ObjectValue")
		Tag.Name = "Creator"
		Tag.Value = Killer
		Tag.Parent = Humanoid
	end
end

local function UntagHumanoid(Humanoid)
	if Humanoid ~= nil then
		local Tag = Humanoid:FindFirstChild("Creator")
		if Tag ~= nil then
			Tag.Parent = nil
		end
	end
end

local function SwingSword()
	local Humanoid = tool.Parent:FindFirstChild("Humanoid") 
	local Animation = Humanoid:LoadAnimation(script.SwordSwing)
	if SwingDebounce == false then
		SwingDebounce = true
		Animation:Play()
		wait(1)
		Animation:Stop()
		SwingDebounce = false
	end
end

local function onTouch(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	local Character = tool.Parent
	local Player = game.Players:GetPlayerFromCharacter(Character)
	if CanDamage.Value == true then
		if Humanoid and DamageDebounce == false then
			if Humanoid.Parent == tool.Parent then return end
			DamageDebounce = true
			TagHumanoid(Humanoid, Player)
			Humanoid:TakeDamage(Damage.Value)
			wait(1)			
			DamageDebounce = false
			wait(1)
			UntagHumanoid(Humanoid)
		end
	end
end

local function OnActivated()
	SwingSword()
	Blade.Touched:Connect(onTouch)
end

tool.Activated:Connect(OnActivated)
1 Like

I assume it has something to do with the CanDamage value. You don’t change the value to true when activating it, and to false when unactivating.
I’m now on mobile, but if you need to have it work I can fix it later today.

the CanDamage is for the player when player in PVPzone

there is another script for the PVP zone , the problem is even the player not Activated the sword , the sword still damage another player when the sword toucehd another player

So you only want the sword to deal damage if they click and not when it’s just in their hand?

You could add a check to see if it is currently swinging, and if it is, then do damage.

local ready = false
local tool = script.Parent
local Blade = tool.Blade
local Handle = tool:WaitForChild(“Handle”)
local SwingDebounce = false
local DamageDebounce = false

local Damage = script.Parent.damage

local CanDamage = tool.Parent.Parent:WaitForChild(“CanDamage”)

local function TagHumanoid(Humanoid,Killer)
if Humanoid and Killer then
local Tag = Instance.new(“ObjectValue”)
Tag.Name = “Creator”
Tag.Value = Killer
Tag.Parent = Humanoid
end
end

local function UntagHumanoid(Humanoid)
if Humanoid ~= nil then
local Tag = Humanoid:FindFirstChild(“Creator”)
if Tag ~= nil then
Tag.Parent = nil
end
end
end

local function SwingSword()
local Humanoid = tool.Parent:FindFirstChild(“Humanoid”)
local Animation = Humanoid:LoadAnimation(script.SwordSwing)
if SwingDebounce == false then
local ready = true
SwingDebounce = true
Animation:Play()
wait(1)
Animation:Stop()
SwingDebounce = false
local ready = false
end
end

local function onTouch(hit)
if ready == true then
local Humanoid = hit.Parent:FindFirstChildWhichIsA(“Humanoid”)
local Character = tool.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
if CanDamage.Value == true then
if Humanoid and DamageDebounce == false then
if Humanoid.Parent == tool.Parent then return end
DamageDebounce = true
TagHumanoid(Humanoid, Player)
Humanoid:TakeDamage(Damage.Value)
wait(1)
DamageDebounce = false
wait(1)
UntagHumanoid(Humanoid)
end
end
end
end

local function OnActivated()
SwingSword()
Blade.Touched:Connect(onTouch)
end

tool.Activated:Connect(OnActivated)

2 Likes

btw you can add ``` to easier to see

1 Like