Need help with sword script

I am making a sword and there are two errors in the script.

So my tool has a part called blade and when the blade is touched to a Humanoid it takes damage.

I want it to take damage only when it’s activated and if a part inside the tool has a transparency of 0.
Apparently non of them are working

Script

if script.Parent.Saber.Transparency > 0 then
	
else
	script.Parent.Activated:Connect(function()
		
		script.Parent.Blade.Touched:connect(function(p)
			
			script.Parent.CanDamage.Value = true
			
			if script.Parent.CanDamage.Value == true then
				
				script.Parent.CanDamage.Value = false
				
				p.Parent.Humanoid:TakeDamage(5)
				
				script.Parent.CanDamage.Value = false
				
			end
			
		end)
	end)
	
end

What could be my possible error?

The other error is pretty easy I think.
I was trying to make my own animations but only when I equip the tool it is working fine but there is a problem.

if script.Parent:WaitForChild("Saber").Transparency == 0 then

	script.Parent.Equipped:Connect(function()
		
		script.Parent.Parent:WaitForChild("Animate").Disabled = true
		
		
		local attack = script.Parent.Parent:WaitForChild("Humanoid"):LoadAnimation(script.Attack)
		local Idle = script.Parent.Parent:WaitForChild("Humanoid"):LoadAnimation(script.Idle)
		local Run = script.Parent.Parent:WaitForChild("Humanoid"):LoadAnimation(script.Run)

		Idle:Play()
		
		local Running = false
		
		local input = game:GetService("UserInputService")

		input.InputBegan:Connect(function(key)	
			if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.S or key.KeyCode == Enum.KeyCode.D then
				Run:Play()
				Idle:Stop()
				attack:Stop()
			end
		end)
		
		input.InputEnded:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.S or key.KeyCode == Enum.KeyCode.D then -- here it is the problem
				Idle:Play()
				Run:Stop()
			end
		end)

		local CanAttack = true
		
		script.Parent.Activated:connect(function()
			
			if CanAttack == true then
				attack:Play()
				Idle:Stop()
				Run:Stop()
				
				wait(1)
				
				Run:Stop()
				attack:Stop()
				Idle:Play()
				
				CanAttack = true
				script.Parent.CanDamage.Value = true
				
				script.Parent.Unequipped:Connect(function()
					Idle:Stop()
					Run:Stop()
					attack:Stop()
				end)
			end
		end)
	end)

elseif script.Parent.Saber.Transparency > 0 then
	print("No Power")
end

Here at line 26 I have marked it as a comment to easily find it.
When a player stop pressing any movement buttons the animation would stop.

But the problem is if a player is pressing two keys and releases one then the animation would stop playing.

How can I fix this?
it’s been 3 days trying to debug this.

1 Like

First script I’d place this line behind the two events like this.

script.Parent.Activated:Connect(function()
	script.Parent.Blade.Touched:connect(function(p)
		if script.Parent.Saber.Transparency > 0 then -- This line inside the events
			script.Parent.CanDamage.Value = true
			if script.Parent.CanDamage.Value == true then
				script.Parent.CanDamage.Value = false
				p.Parent.Humanoid:TakeDamage(5)
				script.Parent.CanDamage.Value = false
			end
		end)
	end)
end

For you movement animation you could use a different event to check if the player is moving.

Player.Character.Humanoid.Running:Connect(function(speed)
	if speed == 0 then
		Run:Play()
		Idle:Stop()
		attack:Stop()
		else
		Idle:Play()
		Run:Stop()
	end
end)
1 Like

Thanks now my player walking fine but the blade isn’t giving any damage
Why is this happening?

To find the problem you can place print() in your code to check if the code works till there and I guess you haven’t shown the whole function of the sword script so if you like you can post the whole code of it.

1 Like