Script isn't working as planned

Can someone help? Im lost in this script.

script.Parent.OnServerEvent:connect(function(Plr, Data)
local c = Plr.Character

local Track = Instance.new("Animation")
Track.AnimationId = "rbxassetid://5759120589"
local Anim = c.Humanoid:LoadAnimation(Track)

local SPEED = c.Humanoid.WalkSpeed
local JUMP = c.Humanoid.JumpPower

if Data == "Block" then
	
	c.Humanoid.WalkSpeed = 0
	c.Humanoid.JumpPower = 0
	Plr.CurrentStats.Blocking.Value = true
	Anim:Play()
	
elseif Data == "Up" then
	
	c.Humanoid.WalkSpeed = SPEED
	c.Humanoid.JumpPower = JUMP
	Plr.CurrentStats.Blocking.Value = false
	Anim:Stop()
	
end

end)

it plays the animation, but doesn’t revert back. Basically my issue is why isn’t the animation stopping?
(No errors btw)
Thanks!

I think it’s because you’re loading a new animation everytime the player fires the event.

Alright, Do you have an example of this? and or an additional resource I could look at?

Try this:

local Animations = {}

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://5759120589"

script.Parent.OnServerEvent:connect(function(Plr, Data)
  local c = Plr.Character
  
  if not Animations[Plr.name] then
    Animations[Plr.Name] = c.Humanoid:LoadAnimation(Animation)
  end

  local SPEED = c.Humanoid.WalkSpeed
  local JUMP = c.Humanoid.JumpPower

  if Data == "Block" then
    c.Humanoid.WalkSpeed = 0
    c.Humanoid.JumpPower = 0
    Plr.CurrentStats.Blocking.Value = true
    Animations[Plr.Name]:Play()
  elseif Data == "Up" then
    c.Humanoid.WalkSpeed = SPEED
    c.Humanoid.JumpPower = JUMP
    Plr.CurrentStats.Blocking.Value = false
    Animations[Plr.Name]:Stop()
  end
end)

Had to change something in the script above, make sure you copied the updated one.

Alright! It has worked! Thank you!

1 Like