I have created a shield in my game but I don’t know know why but the shield is not blocking attacks. Neither my knock back is working nor the damage sounds or the shield is blocking the attacks. I would really appreciate any help I receive in solving this problem. Here is my code and it is not in a local script.
local owner
local character
local charhum
local rootpart
canblockanim = true
equipped = false
runservice = game:GetService("RunService")
tool = script.Parent
handle = tool.Handle
blocksounds = {handle.block, handle.block2, handle.block3, handle.block4}
equipsound = handle.equip
tool.Equipped:connect(function()
equipped = true
end)
tool.Unequipped:connect(function()
equipped = false
end)
tool.Equipped:connect(function()
owner = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
character = owner.Character
charhum = character:findFirstChildOfClass("Humanoid")
rootpart = character.HumanoidRootPart
equipsound:Play()
end)
local thechar = character
local lasthp = charhum.Health
charhum.HealthChanged:connect(function(hp)
if character == thechar and equipped then
if hp < lasthp then
charhum.Health = lasthp
rootpart.Velocity = Vector3.new()
if canblockanim then
local soundplay = math.random(1,#blocksounds)
blocksounds[soundplay]:Play()
local closestdist = math.huge
local knockback = Instance.new("BodyVelocity", rootpart)
knockback.MaxForce = Vector3.new(math.huge,0,math.huge)
knockback.Velocity = rootpart.CFrame.lookVector * -8
game.Debris:AddItem(knockback, 0.15)
end
end
lasthp = charhum.Health
end
end)
local canblockanim = true
local equipped = false
local connection
local players = game:GetService("Players")
local debris = game:GetService('Debris')
local tool = script.Parent
local handle = tool.Handle
local blocksounds = {handle.block, handle.block2, handle.block3, handle.block4}
local equipsound = handle.equip
tool.Equipped:connect(function()
local owner = players:GetPlayerFromCharacter(tool.Parent)
if owner then
local character = owner.Character
if character then
equipsound:Play()
local charhum = character:FindFirstChildOfClass("Humanoid")
local rootpart = character.HumanoidRootPart
local lasthp = charhum.Health
connection = charhum.HealthChanged:Connect(function(hp)
if character and equipped then
if hp < lasthp then
charhum.Health = lasthp
rootpart.Velocity = Vector3.new()
if canblockanim then
local soundplay = math.random(1,#blocksounds)
blocksounds[soundplay]:Play()
end
local knockback = Instance.new("BodyVelocity", rootpart)
knockback.MaxForce = Vector3.new(math.huge,0,math.huge)
knockback.Velocity = rootpart.CFrame.LookVector * -8
debris:AddItem(knockback, 0.15)
end
end
lasthp = charhum.Health
end)
end
end
end)
tool.Unequipped:connect(function()
if connection then
connection:Disconnect()
end
end)
The script is not working but I am not getting any errors either. The humanoid is still receiving damage and the knock back and sound effects are still not happening.
local canblockanim = true
local connection
local players = game:GetService("Players")
local debris = game:GetService('Debris')
local tool = script.Parent
local handle = tool.Handle
local blocksounds = {handle.block, handle.block2, handle.block3, handle.block4}
local equipsound = handle.equip
tool.Equipped:connect(function()
local owner = players:GetPlayerFromCharacter(tool.Parent)
if owner then
local character = owner.Character
if character then
equipsound:Play()
local charhum = character:FindFirstChildOfClass("Humanoid")
local rootpart = character.HumanoidRootPart
local lasthp = charhum.Health
connection = charhum.HealthChanged:Connect(function(hp)
if character then
if hp < lasthp then
charhum.Health = lasthp
rootpart.Velocity = Vector3.new()
if canblockanim then
local soundplay = math.random(1,#blocksounds)
blocksounds[soundplay]:Play()
end
local knockback = Instance.new("BodyVelocity", rootpart)
knockback.MaxForce = Vector3.new(math.huge,0,math.huge)
knockback.Velocity = rootpart.CFrame.LookVector * -8
debris:AddItem(knockback, 0.15)
end
end
lasthp = charhum.Health
end)
end
end
end)
tool.Unequipped:connect(function()
if connection then
connection:Disconnect()
end
end)
It’s working perfectly. Thank you very much but there is one problem which is that the shield is blocking all the attacks from any direction so is there any way by which the shield coverage area can be reduced and the player can be damaged from the back ?