Hi. I am trying to make a crouch button for mobile compatiblity but the animation doesnt stop after the button is clicked twice.
Button click fires a remoteevent which is this:
local event = game.ReplicatedStorage.RemoteEvents.PlayCrouchAnim
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://108365018559262"
event.OnServerEvent:Connect(function(plr, state)
local char = plr.Character
if not char then return end
local hum = char:FindFirstChild("Humanoid")
if not hum then return end
local animator = hum:FindFirstChild("Animator") or Instance.new("Animator", hum)
local animTrack = hum:FindFirstChild("CrouchAnimTrack")
-- Load animation if it's not already loaded
if not animTrack then
animTrack = animator:LoadAnimation(animation)
animTrack.Name = "CrouchAnimTrack"
end
if state == "activate" then
hum.HipHeight = -1
hum.WalkSpeed = 6
animTrack:Play(0.2)
animTrack:AdjustSpeed(0)
-- Update animation speed using RenderStepped
game:GetService("RunService").Heartbeat:Connect(function()
local speed = hum.MoveDirection.Magnitude
animTrack:AdjustSpeed(speed > 0 and 1 or 0)
end)
elseif state == "deactivate" then
hum.HipHeight = 0
hum.WalkSpeed = 16
-- Stop animation
if animTrack then
animTrack:Stop()
end
end
end)
I tried geting AI to fix the issue but it didnt work. And its not a problem with the deactivate state not being fired cuz the hipheight and walkspeed does go back to 0 and 16 so idk why it wont disable the animation.