I’ve been rewriting my code so I can make a block system for it.
I am not currently getting any errors though the Animations and Blocking aren’t working.
code:
local tool = script.Parent
local canDamage = false
local canSwing = true
local Handle = script.Parent:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local Character = player.Character or player.Character:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UserInputType = game:GetService("UserInputType")
local mouse = player:GetMouse()
local block = game:GetService("ReplicatedStorage").Block
local Sword_Block_Animation = Instance.new("Animation")
Sword_Block_Animation.AnimationId = "rbxassetid://9404434731"
local AnimationTrackBlock = Humanoid.Animator:LoadAnimation(Sword_Block_Animation)
local Sword_Walk_Animation = Instance.new("Animation")
Sword_Walk_Animation.AnimationId = "rbxassetid://9404399317"
local AnimationTrackWalk = Humanoid.Animator:LoadAnimation(Sword_Walk_Animation)
local Sword_Idle_Animation = Instance.new("Animation")
Sword_Idle_Animation.AnimationId = "rbxassetid://9404394687"
local AnimationTrackIdle = Humanoid.Animator:LoadAnimation(Sword_Idle_Animation)
local Sword_Equip_Animation = Instance.new("Animation")
Sword_Equip_Animation.AnimationId = "rbxassetid://9537941896"
local AnimationTrackEquip = Humanoid:LoadAnimation(Sword_Equip_Animation)
local shouldPlay = false
local shouldBlock = false
Sounds = {
Slash = Handle:WaitForChild("SlashSound"),
Lunge = Handle:WaitForChild("LungeSound"),
Unsheath = Handle:WaitForChild("EquipSound"),
HitSound1 = Handle:WaitForChild("HitSound1")
}
local Sword_Attack1_Animation = Instance.new("Animation")
Sword_Attack1_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrackAttack1 = Humanoid.Animator:LoadAnimation(Sword_Attack1_Animation)
local Sword_Attack2_Animation = Instance.new("Animation")
Sword_Attack2_Animation.AnimationId = "rbxassetid://9404407784"
local AnimationTrackAttack2 = Humanoid.Animator:LoadAnimation(Sword_Attack2_Animation)
local Sword_Attack3_Animation = Instance.new("Animation")
Sword_Attack3_Animation.AnimationId = "rbxassetid://9404411265"
local AnimationTrackAttack3 = Humanoid.Animator:LoadAnimation(Sword_Attack3_Animation)
local Sword_Attack4_Animation = Instance.new("Animation")
Sword_Attack4_Animation.AnimationId = "rbxassetid://9404416256"
local AnimationTrackAttack4= Humanoid.Animator:LoadAnimation(Sword_Attack4_Animation)
local AnimationsTable = {
[1] = AnimationTrackAttack1,
[2] = AnimationTrackAttack2,
[3] = AnimationTrackAttack3,
[4] = AnimationTrackAttack4
}
local Index = 1
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if not humanoid then
return
end
if humanoid.Parent ~= tool.Parent and canDamage then
humanoid:TakeDamage(5)
Sounds.HitSound1:Play()
else
return
end
canDamage = false
end
local function slash()
if canSwing then
if AnimationsTable[Index] then
AnimationsTable[Index]:Play()
Index += 1
else
Index = 1
AnimationsTable[Index]:Play()
end
canSwing = false
Sounds.Slash:Play()
canDamage = true
wait(1)
canSwing = true
end
end
while shouldBlock do
game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
if not gpe then
if input.UserInputType == Enum.UserInputType.MouseButton2 then
local Anim = script.Block
AnimationTrackBlock:Play()
block:FireServer()
end
end
end)
tool.Equipped:Connect(function()
shouldPlay = true
Sounds.Unsheath:Play()
AnimationTrackEquip:Play()
while shouldPlay do
wait()
Humanoid.Running:Connect(function(speed)
if tool.Parent == player.Backpack then return end
if speed > 0 then
AnimationTrackWalk:Play()
AnimationTrackIdle:Stop()
else
AnimationTrackWalk:Stop()
AnimationTrackIdle:Play()
end
tool.Equipped:Connect(function()
shouldBlock = true
while shouldBlock do
game:GetService("UserInputService").InputBegan:Connect(function(input, gpe)
if not gpe then
if input.UserInputType == Enum.UserInputType.MouseButton2 then
local Anim = script.Block
AnimationTrackBlock:Play()
block:FireServer()
end
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input, gpe)
if gpe then
if input.UserInputType == Enum.UserInputType.MouseButton2 then
AnimationTrackBlock:Stop()
block:FireServer()
end
end
end)
Humanoid.block.Changed:Connect(function()
if Humanoid.block.Value == false then
AnimationTrackBlock:Stop()
end
end)
end
end)
end)
end
end)
tool.Unequipped:Connect(function()
shouldPlay = false
AnimationTrackIdle:Stop()
AnimationTrackWalk:Stop()
shouldBlock = false
if tool.Parent == player.Backpack then return end
Humanoid.block.Changed:Connect(function()
if Humanoid.block.Value == false then
AnimationTrackBlock:Stop()
end
end)
end)
end
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
Any help would be very much appreciated! If you need more details I will provide.