So I to make my character block any attacks or damages he encounters by pressing Q and when it blocks, it'll play a little animation and start to block for a limited amount. But I don't know how to make the player reduce health damage when some sort of an enemy NPC decides to attack you. I don't want to add health onto player.
--= Variables =--
local UIS = game:GetService('UserInputService')
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')
local player = game.Players.LocalPlayer
local NormalWalkSpeed = game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed
local NewWalkSpeed = NormalWalkSpeed * 0.5
local NormalHealthReduce = --???
local NewHealthReduce = --???
local power = 10
local sprinting = false
local blocking = false
repeat wait() until game.Players.LocalPlayer.Character
local character = player.Character or player.CharacterAdded:wait()
--= Functions =--
UIS.InputBegan:connect(function(key, gameProcessed)
if key.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://2610577749"
animation = character.Humanoid:LoadAnimation(animation)
animation:Play() ----- Active animation
blocking = true
-- Here Code
character.Humanoid.WalkSpeed = NewWalkSpeed
sprinting = true
while power > 0 and sprinting and blocking do
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://2610584736"
animation = character.Humanoid:LoadAnimation(animation)
animation:Play() ----- Active animation
power = power - .075
Bar:TweenSize(UDim2.new(1, 0, power / 10, 0), 'Out', 'Quint', .1, true)
--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 42, 42), 0.001)
wait()
if power <= 0 then
character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end)
UIS.InputEnded:connect(function(key, gameProcessed)
if key.KeyCode == Enum.KeyCode.Q and gameProcessed == false then
character.Humanoid.WalkSpeed = NormalWalkSpeed
sprinting = false
blocking = false
while power < 10 and not sprinting and not blocking do
power = power + .025
Bar:TweenSize(UDim2.new(1, 0, power / 10, 0), 'Out', 'Quint', .1, true)
--Bar.BackgroundColor3 = Bar.BackgroundColor3:lerp(Color3.fromRGB(255, 166, 11), 0.001)
wait()
if power <= 0 then
character.Humanoid.WalkSpeed = NormalWalkSpeed
end
end
end
end)

3 comments
ok
ok
I feel like some games know how to block but there were no tutorials, so I tried to do it with a shield script, but didn't work anyways