I am trying to create a “blocking” script that makes you take no damage when holding F. I have the animation part done, and all of the variables, but I cannot seem to figure out the damage. Nothing (besides the prints) are shown in the output, so I do not know what the problem is.
Here is the part of the script that handles damage:
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local hp = hum.Health
local function onHealthChanged(health)
print("took damage")
hp = health
if isBlocking then
hum.Health = health
print(health)
end
end
if hum then
hum.HealthChanged:Connect(onHealthChanged)
end
char.Humanoid.HealthChanged:Connect(onHealthChanged)
Here is my full script:
local UserInputService = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local char = plr.Character
local events = rs:WaitForChild("Events")
local contblock = events:WaitForChild("Block")
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local hp = hum.Health
local contblockAnim = animator:LoadAnimation(script:WaitForChild("ContBlock"))
local isBlocking = false
local function playAnimation()
if not isBlocking then
isBlocking = true
contblockAnim:Play()
end
end
local function stopAnimation()
if isBlocking then
isBlocking = false
contblockAnim:Stop()
end
end
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.F then
playAnimation()
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.F then
stopAnimation()
end
end)
local function onHealthChanged(health)
print("took damage")
hp = health
if isBlocking then
hum.Health = health
print(health)
end
end
if hum then
hum.HealthChanged:Connect(onHealthChanged)
end
char.Humanoid.HealthChanged:Connect(onHealthChanged)
local blockEvent = rs.Events:FindFirstChild("Block")
Please help if you can, thank you!