Sword still taking damage when not clicked

Ye the title says it all

local Damage = false
local attackTime = 1



script.Parent.Event.OnServerEvent:Connect(function(Player, Tool)


	
	Tool.Handle.Blade.Touched:Connect(function(hit)
	
		
		if hit.Parent:FindFirstChild("Humanoid") then 
			

			local CloneOfVisualOne = game.ServerStorage.VisualOne:Clone()
			local TweenService = game:GetService("TweenService")
			local Tweeninfo = TweenInfo.new(1.0)
			local TweenEnds = {
				Transparency = 1,
				Size = Vector3.new(11.2, 7.8, 11.2)
			}
			local Tween = TweenService:Create(CloneOfVisualOne, Tweeninfo, TweenEnds)
			
			if attackTime then return end isAttacking = not isAttacking 
			isAttacking = true
			local character = hit.Parent 
			CloneOfVisualOne.CFrame = character.HumanoidRootPart.CFrame
			CloneOfVisualOne.Parent = workspace
			Tween:Play()
			character.Humanoid:TakeDamage(1)
			wait(attackTime) -- assuming you don't already have a wait in the code
			attackTime = false
CloneOfVisualOne:Destroy()
				
		end
	end)
end)
2 Likes

I kept mentioning that do not connect functions in a function. Instead, you would toggle a variable to see whether the player is slashing or not. If they are slashing and the sword touches something, check if the sword is slashing and then proceed to deal damage as per usual.

3 Likes

Maybe this? You set isAttacking as a variable to it’s opposite, but in the next line make it true anyway?

2 Likes

what , xd also i made the code shorter to help but this still doesnt work

    local isAttacking = false
    local attackTime = 1


    script.Parent.Event.OnServerEvent:Connect(function(Player, Tool)
        Tool.Handle.Blade.Touched:Connect(function(hit)
            if isAttacking then return end isAttacking = not isAttacking 
            isAttacking = true
            if hit.Parent:FindFirstChild("Humanoid") then 
                local character = hit.Parent 
                character.Humanoid:TakeDamage(5)
                wait(attackTime) -- assuming you don't already have a wait in the code
                isAttacking = false
            

            end
        end)
    end)
1 Like

What you can do is create a table, when they activate the sword add them to the table and then wait around one second and remove them from the table. Here is an example code:

local table = {}

script.Parent.Event.OnServerEvent:Connect(function(Player, Tool)
   if not table[Player.Name] and not cooldown then
      cooldown = true
      table.insert(table, Player.Name)
      -- damage player
      wait(cooldownTime)
      table[Player.Name] = nil
   end
end)

You’ll need to disconnect the touched event after it’s used (or use :Wait instead of :Connect), but I’d recommend using a bool instead of keep creating an event once a remote has been fired.

local Damage = false
local attackTime = 1

local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new(1.0)

script.Parent.Event.OnServerEvent:Connect(function(Player, Tool)
	local hit = Tool.Handle.Blade.Touched:Wait()			

	if hit.Parent:FindFirstChild("Humanoid") then 			
		local CloneOfVisualOne = game.ServerStorage.VisualOne:Clone()
		local TweenEnds = {
			Transparency = 1,
			Size = Vector3.new(11.2, 7.8, 11.2)
		}

		local Tween = TweenService:Create(CloneOfVisualOne, Tweeninfo, TweenEnds)
			
		if attackTime then return end isAttacking = not isAttacking 
		isAttacking = true
		local character = hit.Parent 
		CloneOfVisualOne.CFrame = character.HumanoidRootPart.CFrame
		CloneOfVisualOne.Parent = workspace
		Tween:Play()
		character.Humanoid:TakeDamage(1)
		wait(attackTime)
		attackTime = false
        CloneOfVisualOne:Destroy()			
	end
end)
2 Likes