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

How to make a blocking system on roblox?

so I have made the animation work just fine but I cant seem to get the script working so that if a player is blocking and they were hit they take less damage.

here is my animation script:

local UIS = game:GetService('UserInputService')
local BlockAnim = script:WaitForChild('BlockAnim')
local KeyDown = false
local Hum = game.Players.LocalPlayer.Character.Humanoid
local anim = Hum:LoadAnimation(BlockAnim)
local tool = game.StarterPack.ClassicSword
local char = game.Players.LocalPlayer.Character
local isBlocking = tool.Handle.IsBlocking.Value

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		KeyDown = true
	end
	while KeyDown == true do
		isBlocking = true
		print(isBlocking)
		game.StarterPlayer.CharacterWalkSpeed = 0
		game.StarterPlayer.CharacterJumpPower = 25
		anim:Play()
		wait(.01)
	end
end)

		

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		KeyDown = false
	end
	while KeyDown == false do
		isBlocking = false
		print(isBlocking)
		game.StarterPlayer.CharacterWalkSpeed = 16
		game.StarterPlayer.CharacterJumpPower = 50
		anim:Stop()
		wait(.1)
	end
end)

I made a variable for if the player is blocking (isBlocking) this might help you. idk i also have no idea on where to start.

Sword Script:

local cooldown = script.Parent.Parent.CoolDown.Value
local tool = script.Parent.Parent
local isblocking = tool.Handle.IsBlocking.Value

script.Parent.Touched:Connect(function(hit)
	script.Parent.Parent.Activated:Connect(function()
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		if Humanoid and cooldown == false then
			Humanoid:TakeDamage(7)
			cooldown = true
			wait(.5)
			cooldown = false
		end
	end)
end)


2 answers

✓ Accepted answer

For a server script, do:

local RS = game:GetService("ReplicatedStorage")
local BlockEvent = RS:WaitForChild("BlockEvent")
local DamageEvent = RS:WaitForChild("DamageEvent")

DamageEvent.OnServerEvent:Connect(function(Player, OtherPlayer, Damage)
	OtherPlayer.Character.Humanoid:TakeDamage(Damage)
end)

BlockEvent.OnServerEvent:Connect(function(Player, Blocked)
	if OtherCharacter:FindFirstChild("ClassicSword") then
		OtherTool = OtherCharacter.ClassicSword
	elseif Players[OtherCharacter.Name].Backpack:FindFirstChild("ClassicSword") then
		OtherTool = Players[OtherCharacter.Name].Backpack.ClassicSword
	end
	OtherTool.IsBlocking.Value = Blocked
end)

Make two events, one called "BlockEvent" and one called "DamageEvent" and place them in ReplicatedStorage

Now delete the animation script and replace the sword script with this:

local UIS = game:GetService('UserInputService')
local Cooldown = false
local BlockAnim = script:WaitForChild('BlockAnim')
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Anim = Hum:LoadAnimation(BlockAnim)
local Tool = script.Parent
local IsBlocking = tool.Handle.IsBlocking
local RS = game:GetService("ReplicatedStorage")
local BlockEvent = RS:WaitForChild("BlockEvent")
local DamageEvent = RS:WaitForChild("DamageEvent")
local OtherCharacter, OtherTool
anim.Looped = true

local RegDamage = 7
local BlockDamage = 2

function GetTouchingParts(part)
   local connection = part.Touched:Connect(function() end)
   local results = part:GetTouchingParts()
   connection:Disconnect()
   return results
end

UIS.InputBegan:Connect(function(Input, Processed)
	if Processed then
		return
	elseif input.KeyCode == Enum.KeyCode.Q then
		Anim:Play()
		Character.Humanoid.WalkSpeed = 0
		Character.Humanoid.JumpPower = 25
		BlockEvent:FireServer(true)
    end
end)

UIS.InputEnded:Connect(function(Input, Processed)
	if Processed then
		return
	elseif input.KeyCode == Enum.KeyCode.Q then
		Anim:Stop()
		Character.Humanoid.WalkSpeed = 16
		Character.Humanoid.JumpPower = 50
		BlockEvent:FireServer(false)
    end
end)

Tool.Activated:Connect(function()
	if not Cooldown then
		Cooldown = true
		OtherCharacter = nil
		OtherTool = nil
		for Index, Part in pairs(GetTouchingParts(Tool.Handle)) do
			if Part.Parent:FindFirstChild("Humanoid") and Part.Parent.Name ~= Player.Name then
				OtherCharacter = Part.Parent
			end
		end
		if OtherCharacter:FindFirstChild("ClassicSword") then
			OtherTool = OtherCharacter.ClassicSword
		elseif Players[OtherCharacter.Name].Backpack:FindFirstChild("ClassicSword") then
			OtherTool = Players[OtherCharacter.Name].Backpack.ClassicSword
		end
		if OtherTool.IsBlocking.Value then
			DamageEvent:FireServer(Players[OtherCharacter.Name], BlockDamage)
			OtherCharacter.Humanoid:TakeDamage(BlockDamage)
		else
			DamageEvent:FireServer(Players[OtherCharacter.Name], RegDamage)
			OtherCharacter.Humanoid:TakeDamage(RegDamage)
		end
		wait(.5)
		Cooldown = false
	end
end)

I'm really tired and about to go to sleep so if you run into any problems, just comment and i'll help in the morning. If you need an explanation of the script, i'll also do one in the morning

This is really a good start. Next thing, you have to send the blocking request to the server, and make all attacks detect if the player is blocking, and deal less damage/no damage, with a special animation.

Try making all characters have a Blocking boolvalue inside of them. Call the server to turn this bool on or off. Attacks should check.. if Blocking then else..

1 comment

0

Im not that good at coding so could u please show a example code of it?

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