I tried to add blocking to a sword script and it didn’t work at all. Just wondering if anyone could fix it or anything. Thanks!
local r = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local damage = 0
local slash_damage = 10
local lunge_damage = 20
local sword = script.Parent.Handle
local Tool = script.Parent
local damages, values, sounds = {10, 11, 12}, {Tool.PlaySlash, Tool.PlayThrust, Tool.PlayOverhead}, {Tool.Handle.SlashSound, Tool.Handle.OverheadSound, Tool.Handle.LungeSound}
local enabledToDamage = true
local isBlocking = false
local isParrying = false
function blow(hit)
if not enabledToDamage then return end
enabledToDamage = false
if hit.Parent == nil then enabledToDamage = true return end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local vCharacter = Tool.Parent
local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
local hum = vCharacter:FindFirstChild("Humanoid")
if humanoid and humanoid ~= hum and hum then
local right_arm = vCharacter:FindFirstChild("Right Arm")
if right_arm then
local joint = right_arm:FindFirstChild("RightGrip")
if joint and (joint.Part0 == sword or joint.Part1 == sword) then
if isBlocking then
-- Play block animation
vCharacter.Humanoid:LoadAnimation(script.BlockAnimation):Play()
elseif isParrying then
-- Play parry animation and reduce damage
vCharacter.Humanoid:LoadAnimation(script.ParriedAnimation):Play()
damage = damage / 2
else
tagHumanoid(humanoid, vPlayer)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(humanoid)
end
end
end
end
enabledToDamage = true
end
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid then
local tag = humanoid:FindFirstChild("creator")
if tag then
tag.Parent = nil
end
end
end
function attack()
damage = slash_damage
script.Parent.Handle.SlashSound:Play()
script.Parent.PlaySlash.Value = not script.Parent.PlaySlash.Value
end
function lunge()
damage = lunge_damage
script.Parent.Handle.LungeSound:Play()
script.Parent.PlayOverhead.Value = not script.Parent.PlayOverhead.Value
local force = Instance.new("BodyVelocity")
force.Velocity = Vector3.new(0, 10, 0)
force.Parent = Tool.Parent:FindFirstChild("Torso")
wait(0.5)
force.Parent = nil
wait(0.5)
damage = slash_damage
end
Tool.Enabled = true
local last_attack = 0
local status = 0
function onActivated()
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
print("Humanoid not found")
Tool.Enabled = true
return
end
local t = r.Stepped:Wait()
if t - last_attack < 1.5 then
if status == 3 then
status = 0
damage = 0
else
status = status + 1
values[status].Value = not values[status].Value
damage = damages[status]
sounds[status]:Play()
enabledToDamage = true
wait(0.5)
enabledToDamage = false
end
else
status = 0
damage = 0
end
last_attack = t
Tool.Enabled = true
end
function onEquipped()
wait(1/3)
Tool.Handle.UnsheathSound:Play()
end
function onKeyPress(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.F then
isBlocking = true
local anim = Tool.Parent.Humanoid:LoadAnimation(script.BlockAnimation)
anim:Play()
end
end
function onKeyRelease(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.F then
isBlocking = false
-- Stop the block animation if needed
-- anim:Stop()
end
end
Tool.Equipped:Connect(onEquipped)
script.Parent.Activated:Connect(onActivated)
local connection = sword.Touched:Connect(blow)
uis.InputBegan:Connect(onKeyPress)
uis.InputEnded:Connect(onKeyRelease)
Again, thanks!