Hello! I’m making a blocking system for my game but I can’t get it the animation to freeze but I also don’t want the player to be stuck in place. It’s like those battlegrounds game where the player holds “F” to block and when the animation is completed, it will stay like that until the player lets go of F.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Block_Event = script.Parent
local Animations = ReplicatedStorage:WaitForChild("Animations")
local Hand_Block = Animations:WaitForChild("Hand_Block")
local Sword_Block = Animations:WaitForChild("Sword_Block")
local Change_Block_Part_CFrame_Connection
local Current_Playing_Block_Animation_Track
local function Create_Block_Weld_Constraint(character, HRP, Block_Part)
local Weld_Constraint = Instance.new("WeldConstraint")
Weld_Constraint.Name = "Block_Weld_Constraint"
Weld_Constraint.Part0 = HRP
Weld_Constraint.Part1 = Block_Part
Weld_Constraint.Parent = character
end
local function Create_Block_Part()
local Block_Part = Instance.new("Part")
Block_Part.Massless = true
Block_Part.CustomPhysicalProperties = PhysicalProperties.new(0.001)
Block_Part.Name = "Block_Part"
Block_Part.CastShadow = false
Block_Part.Material = Enum.Material.Neon
Block_Part.Color = Color3.fromRGB(255, 0, 0)
Block_Part.Transparency = 0.65
Block_Part.CanTouch = false
Block_Part.CanCollide = false
Block_Part.Size = Vector3.new(4, 5.25, 2.5)
return Block_Part
end
local function Update_Block_Part_CFrame(HRP, Block_Part)
Block_Part.CFrame = HRP.CFrame * CFrame.new(0, 0, -2)
end
local function Create_Animation_Track(Block_Animation, Animator)
local Block_Animation_Clone:Animation = Block_Animation:Clone()
Block_Animation_Clone.Parent = Animator
Current_Playing_Block_Animation_Track = Animator:LoadAnimation(Block_Animation_Clone)
Current_Playing_Block_Animation_Track.Priority = Enum.AnimationPriority.Action
end
local function On_Event(player, Blocking_Boolean)
local character = player.Character
local Humanoid = character:WaitForChild("Humanoid")
local HRP = character:WaitForChild("HumanoidRootPart")
local Animator = Humanoid:WaitForChild("Animator")
if not player or not character or not Humanoid or not HRP or Humanoid.Health <= 0 or typeof(Blocking_Boolean) ~= "boolean" then return end
if Blocking_Boolean == false then -- If player lets go of Block Key then destroy Block_Part and Block_Weld_Constraint
local Block_Part = character:FindFirstChild("Block_Part")
local Block_Weld_Constraint = character:FindFirstChild("Block_Weld_Constraint")
-- Add a lot of checks to ensure that the player is blocking before unblocking
if Block_Part and Block_Weld_Constraint and Change_Block_Part_CFrame_Connection ~= nil and Current_Playing_Block_Animation_Track ~= nil and Animator:FindFirstChildOfClass("Animation") then return end
Block_Part:Destroy()
Block_Weld_Constraint:Destroy()
Change_Block_Part_CFrame_Connection:Disconnect()
Change_Block_Part_CFrame_Connection = nil
Current_Playing_Block_Animation_Track:AdjustSpeed(-1)
Current_Playing_Block_Animation_Track.Ended:Wait()
Current_Playing_Block_Animation_Track:Destroy()
return
end
local Block_Part = Create_Block_Part()
local Block_Weld_Constraint = Create_Block_Weld_Constraint(character, HRP, Block_Part)
Block_Part.CFrame = HRP.CFrame * CFrame.new(0, 0, -2)
Block_Part.Parent = character
Change_Block_Part_CFrame_Connection = HRP:GetPropertyChangedSignal("CFrame"):Connect(function()
Update_Block_Part_CFrame(HRP, Block_Part)
end)
if character:FindFirstChild("ClassicSword") then
Create_Animation_Track(Sword_Block, Animator)
Current_Playing_Block_Animation_Track:Play()
task.wait(Current_Playing_Block_Animation_Track.Length)
Current_Playing_Block_Animation_Track:AdjustSpeed(0)
else
Create_Animation_Track(Hand_Block, Animator)
Current_Playing_Block_Animation_Track:Play()
task.wait(Current_Playing_Block_Animation_Track.Length)
Current_Playing_Block_Animation_Track:AdjustSpeed(0)
end
end
Block_Event.OnServerEvent:Connect(function(player, Block_Status)
On_Event(player, Block_Status)
end)
In my code, I tried to make it so it wil wait for the Animation’s TimeLength and then it freezes it; however, my animation still resets. I’ve also tried waiting for a close time that is almost around the end of the animation but it looks terrible. I don’t know how to fix this?
(Not needed but nice: Feedback on meh code >:D)
