Why is my Sword block Event not working?

I’m trying to make a sword, and I’ve tried many things but none seem to work, so I tried using remote events, and it works for the most part, but breaks halfway through, Basically it sends a remote event from the client to server when the MB2 input began, and another one when MB2 input ends, but it doesn’t seem to send the second one.

Local Script:

 local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")



local tool = script.Parent
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local isEquipped = false
local BlockEvent = script.Parent.BlockRemote
local BlockStopEvent = script.Parent.BlockStopRemote


-- Ensure that the character's humanoid contains an "Animator" object
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

-- Create a new "Animation" instance and assign an animation asset ID
local kickAnimation = Instance.new("Animation")
kickAnimation.AnimationId = "rbxassetid://16020632123"

-- Load the animation onto the animator
local kickAnimationTrack = animator:LoadAnimation(kickAnimation)

local function onEquipped(_mouse)
   isEquipped = true
   print("The tool was equipped")
end
tool.Equipped:Connect(onEquipped)

tool.Unequipped:Connect(function()
   isEquipped = false
   print("The tool was unequipped")
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
   if input.UserInputType == Enum.UserInputType.MouseButton2 then
   	kickAnimationTrack:Play()
   	BlockEvent:FireServer()
   end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
   if input.UserInputType == Enum.UserInputType.MouseButton2 then
   	BlockStopEvent:FireServer()
   	kickAnimationTrack:Stop()
   end
end)

Server Script:

--CONFIGURATION
local Turn = 1
local Damage = 35
local Cooldown = 2

local BlockEvent = script.Parent.BlockRemote
local BlockStopEvent = script.Parent.BlockStopRemote

--END
local UserInputService = game:GetService("UserInputService")
local tool = script.Parent
local mesh = script.Parent.Handle

script.Parent.Activated:Connect(function()
   if script.Parent.Cooldown.Value == false then
   	script.Parent.Cooldown.Value = true
   	script.Parent.CanDamage.Value = true

   	local Humanoid = script.Parent.Parent.Humanoid
   	local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
   	local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
   	local Anim3 = Humanoid:LoadAnimation(script.Parent.Attack3)
   	local Slash = script.Parent.Slash


   	if Turn == 1 then
   			Anim1:Play()
   			Turn = 2

   			Anim1.Stopped:wait(Cooldown)
   			script.Parent.CanDamage.Value = false
   			script.Parent.Cooldown.Value = false
   			wait(.3)
   			Slash:Play()
   	elseif Turn == 2 then
   			Anim2:Play()
   			Turn = 3

   			Anim2.Stopped:Wait(Cooldown)
   			script.Parent.CanDamage.Value = false
   			script.Parent.Cooldown.Value = false
   	elseif Turn == 3 then
   			Anim3:Play()
   			Turn = 1

   			Anim3.Stopped:Wait(Cooldown)
   			script.Parent.CanDamage.Value = false
   			script.Parent.Cooldown.Value = false
   			wait(.2)
   			Slash:Play()
   	end
   else
   	return
   end
end)

script.Parent.Handle.Touched:Connect(function(Hit)
   if Hit.Parent:FindFirstChild("Humanoid") then
   	local Humanoid = Hit.Parent.Humanoid

   	if script.Parent.CanDamage.Value == true then
   		Humanoid:TakeDamage(Damage)
   		script.Parent.CanDamage.Value = false
   	else
   		return
   	end
   else
   	return
   end
end)

local function Block ()
   print("block test!!!")
   script.Parent.Blocking.Value = true
   forceField = Instance.new("ForceField")
   forceField.Parent = script.Parent.Parent
end

BlockEvent.OnServerEvent:Connect(Block)

local function UnBlock ()
   print("unblock test!!!")
   script.Parent.Blocking.Value = false
   forceField:Destroy()
end

BlockStopEvent.OnServerEvent:Connect(Block)

while true do
   wait(1)	
   print(script.Parent.Blocking.Value)
end

Sword in workspace:

Screenshot 2024-01-18 205403

Video:

by the way the force field is supposed to be destroyed if it wasnt obvious

2 Likes

and another one when MB2 input ends, but it doesn’t seem to send the second one

Prioritize your blockevent over the animation.

UserInputService.InputBegan:Connect(function(input, gameProcessed)
   if input.UserInputType == Enum.UserInputType.MouseButton2 then
   	kickAnimationTrack:Play()
   	BlockEvent:FireServer()
   end
end)

if this doesn’t work please respond.

1 Like

No sadly it didn’t work but thanks for the quick reply! :smile:

1 Like

Would you mind giving me the script that intercepts when the event is fired?

Sure, but the server script is the one that picks it up, i already showed it above, anyways here it is:

--CONFIGURATION
local Turn = 1
local Damage = 35
local Cooldown = 2

local BlockEvent = script.Parent.BlockRemote
local BlockStopEvent = script.Parent.BlockStopRemote

--END
local UserInputService = game:GetService("UserInputService")
local tool = script.Parent
local mesh = script.Parent.Handle

script.Parent.Activated:Connect(function()
	if script.Parent.Cooldown.Value == false then
		script.Parent.Cooldown.Value = true
		script.Parent.CanDamage.Value = true

		local Humanoid = script.Parent.Parent.Humanoid
		local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
		local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
		local Anim3 = Humanoid:LoadAnimation(script.Parent.Attack3)
		local Slash = script.Parent.Slash


		if Turn == 1 then
				Anim1:Play()
				Turn = 2

				Anim1.Stopped:wait(Cooldown)
				script.Parent.CanDamage.Value = false
				script.Parent.Cooldown.Value = false
				wait(.3)
				Slash:Play()
		elseif Turn == 2 then
				Anim2:Play()
				Turn = 3

				Anim2.Stopped:Wait(Cooldown)
				script.Parent.CanDamage.Value = false
				script.Parent.Cooldown.Value = false
		elseif Turn == 3 then
				Anim3:Play()
				Turn = 1

				Anim3.Stopped:Wait(Cooldown)
				script.Parent.CanDamage.Value = false
				script.Parent.Cooldown.Value = false
				wait(.2)
				Slash:Play()
		end
	else
		return
	end
end)

script.Parent.Handle.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Hit.Parent.Humanoid

		if script.Parent.CanDamage.Value == true then
			Humanoid:TakeDamage(Damage)
			script.Parent.CanDamage.Value = false
		else
			return
		end
	else
		return
	end
end)

local function Block ()
	print("block test!!!")
	script.Parent.Blocking.Value = true
	forceField = Instance.new("ForceField")
	forceField.Parent = script.Parent.Parent
end

BlockEvent.OnServerEvent:Connect(Block)

local function UnBlock ()
	print("unblock test!!!")
	script.Parent.Blocking.Value = false
	forceField:Destroy()
end

BlockStopEvent.OnServerEvent:Connect(Block)

while true do
	wait(1)	
	print(script.Parent.Blocking.Value)
end

once again, thanks for the quick response

oh yeah and i forgot to mention, when i let go of right click it fires the input began function again, sometimes 3 times.

local Damage = 35
local Cooldown = 2

local BlockEvent = script.Parent.BlockRemote
local BlockStopEvent = script.Parent.BlockStopRemote

local UserInputService = game:GetService("UserInputService")
local tool = script.Parent
local mesh = script.Parent.Handle

local function getRandomTurn()
    return math.random(1, 3)
end

script.Parent.Activated:Connect(function()
   if script.Parent.Cooldown.Value == false then
   	script.Parent.Cooldown.Value = true
   	script.Parent.CanDamage.Value = true

   	local Humanoid = script.Parent.Parent.Humanoid
   	local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
   	local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
   	local Anim3 = Humanoid:LoadAnimation(script.Parent.Attack3)
   	local Slash = script.Parent.Slash

   	local player = game.Players:FindFirstChild(script.Parent.Parent.Name)

   	if player then
   		local turnValue = player:FindFirstChild("Turn")

   		local Turn = turnValue and turnValue.Value or getRandomTurn()

   		if not turnValue then
   			turnValue = Instance.new("IntValue")
   			turnValue.Name = "Turn"
   			turnValue.Parent = player
   		end
   		turnValue.Value = Turn

   		if Turn == 1 then
   			Anim1:Play()
   		elseif Turn == 2 then
   			Anim2:Play()
   		elseif Turn == 3 then
   			Anim3:Play()
   		end

   		local currentAnim = Turn == 1 and Anim1 or Turn == 2 and Anim2 or Anim3

   		currentAnim.Stopped:Wait(Cooldown)
   		script.Parent.CanDamage.Value = false
   		script.Parent.Cooldown.Value = false

   		wait(.2)
   		Slash:Play()
   	end
   else
   	return
   end
end)

script.Parent.Handle.Touched:Connect(function(Hit)
   if Hit.Parent:FindFirstChild("Humanoid") then
   	local Humanoid = Hit.Parent.Humanoid

   	if script.Parent.CanDamage.Value == true then
   		Humanoid:TakeDamage(Damage)
   		script.Parent.CanDamage.Value = false
   	else
   		return
   	end
   else
   	return
   end
end)

local forceField

local function Block()
   print("block test!!!")
   script.Parent.Blocking.Value = true
   forceField = Instance.new("ForceField")
   forceField.Parent = script.Parent.Parent
end

BlockEvent.OnServerEvent:Connect(Block)

local function UnBlock()
   print("unblock test!!!")
   script.Parent.Blocking.Value = false
   if forceField then
      forceField:Destroy()
   end
end

BlockStopEvent.OnServerEvent:Connect(UnBlock)

while true do
   wait(1)	
   print(script.Parent.Blocking.Value)
end
1 Like

Oh my god I am so dumb, sorry to waste your time, it turns out i had the blockEndEvent connect to the first function sorry!!! :sweat_smile:

My apologies for confusing you as well, you can still implement the randomness system I outlined above, unless you already have one.

No I appreiecate it, its just a combo system like strongets battle grounds

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.