Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to reduce player health damage when blocking?

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

0

ok

0

ok

0

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

2 answers

One way you could go about doing this (and I'm not sure if this is the most efficient, but rather a way to do it for now until you can come back to it with more knowledge), is putting a 'BoolValue' inside of the player model called "Blocking". When the player holds "Q", simply change the value to true. Clearly that doesn't help alone, but if you added an if statement to your damage script that checks "if hit.Parent.Blocking then do less damage" I think it could work quite well.

Tell me how this works out, I don't have access to studio right now but I'm very curious to see if this method works like I'm imagining it to.

Hope I helped!

-Cmgtotalyawesome

2 comments

0

when they are blocking simply deal less damage

0

But how do I make my character get less damage from NPC's

so the humanoid has a health changed function i believe; we can find the amount of changed health, divide by 2 and restore it back to the player. However, since health can only be changed using server scripts well have to use 2 scripts:

in a Script in StarerPack:

local hp01 = Instance.new("RemoteEvent", script)
hp01.Name = "hp01"

hp01.OnServerEvent:Connect(function(plr, hum, hp04)
	hum.Health = hum.Health + hp04 / 2
end)

in a LocalScript in the Script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local holding = false

local hp01 = script.Parent:WaitForChild("hp01")

mouse.KeyDown:Connect(function(key)
	if key == "q" then holding = true
		local hum = plr.Character.Humanoid
		
		while holding do
			local hp02 = hum.Health
			
			wait()
			local hp03 = hum.Health
			
			if hp02 > hp03 then
				local hp04 = hp02 - hp03
				hp01:FireServer(hum, hp04)
			end
		end
	end
end)

mouse.KeyUp:Connect(function(key)
	if key == "q" then holding = false end
end)

also i ended up not using the health changed function and just wrote a function which checks for change(specifically decrease) in health. That way its much more easier to check for the exact amount of decrease for, e.g, consecutive damage.

6 comments

0

He said he doesn't want to add health back to the player (not sure if he meant while blocking or after taking damage, but figured I should let you know before you write that script out)

0

o ok ye i havent yet thx

0

also im pretty sure he meant blocking

0

worth a shot

0

Well it does kinda work but whenever I sometimes let go of the "q", it'll make the health full, but I need my character with his original low health

0

ok after some editing i wasnt able to replicate ur result. Ill post the new script in above; also could u see wether u could do that with the new scripts?

This discussion is preserved as it was. New answers, comments, and votes are closed.