Switch between animations

Since I’m not home right now waiting for something to done, I decided to test my game from my phone to kill some time… and then I noticed the bad switching between animations… for example, I’m standing and the idle animation is working, then I start walking, I’m actually walking but the idle animation is on, then after about 3 seconds the walking animation starts working and the idle animation stops… and this happens every time the character’s movement state is changed. What could be the problem? Is there a way to make it smoother?

local ReplicatedStorage = game:GetService("ReplicatedStorage")local PlayAnimationEvent = ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("PlayAnimationEvent")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- استيراد المعرفات من ModuleScript داخل الموديلlocal AnimationReferences = require(script:WaitForChild("ModuleScript"))
-- إنشاء الأنيميشنات
local jumpAnim = Instance.new("Animation")jumpAnim.AnimationId = AnimationReferences.JumpAnimId
local runAnim = Instance.new("Animation")
runAnim.AnimationId = AnimationReferences.RunAnimId
local walkAnim = Instance.new("Animation")walkAnim.AnimationId = AnimationReferences.WalkAnimId  -- إضافة أنيميشن المشي
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = AnimationReferences.IdleAnimId
local fallAnim = Instance.new("Animation")fallAnim.AnimationId = AnimationReferences.FallAnimId -- إضافة أنيميشن السقوط
-- تحميل الأنيميشنات باستخدام Animator
local jumpAnimTrack = animator:LoadAnimation(jumpAnim)local runAnimTrack = animator:LoadAnimation(runAnim)
local walkAnimTrack = animator:LoadAnimation(walkAnim)local idleAnimTrack = animator:LoadAnimation(idleAnim)
local fallAnimTrack = animator:LoadAnimation(fallAnim)
-- متغير لتخزين الأنيميشن الحاليlocal currentAnimTrack
local transitionTime = 0.2  -- زمن الانتقال بين الأنيميشنات
-- دالة لإيقاف الأنيميشنات السابقة قبل بدء الأنيميشن الجديدlocal function stopAllAnimations()
 for _, track in pairs(animator:GetPlayingAnimationTracks()) do  track:Stop(transitionTime)
 endend
-- دالة لتعديل سرعة الأنيميشن
local function adjustAnimation(name, speed, weight) local track = animTracks[name]
 if track then  track:AdjustSpeed(speed)
  track:AdjustWeight(weight) end
end
-- دالة لتشغيل الأنيميشنlocal function playAnimation(name, speed, weight)
 local track = animTracks[name] track:Play(transitionTime)
 adjustAnimation(name, speed, weight) stopAllAnimations()  -- إيقاف الأنيميشنات السابقة
end
-- الاستماع للأحداث المرسلة من السيرفر وتشغيل الأنيميشناتPlayAnimationEvent.OnClientEvent:Connect(function(animType)
 stopAllAnimations()  -- إيقاف الأنيميشن الحالي قبل تشغيل الأنيميشن الجديد
 -- تحديد سرعة الأنيميشن بناءً على نوع الحدث (ركض أو مشي أو غيره) if animType == "JumpAnim" then
  currentAnimTrack = jumpAnimTrack  jumpAnimTrack:Play()
 elseif animType == "RunAnim" then  currentAnimTrack = runAnimTrack
  runAnimTrack:Play(transitionTime) elseif animType == "WalkAnim" then
  currentAnimTrack = walkAnimTrack  walkAnimTrack:Play(transitionTime)
 elseif animType == "IdleAnim" then  currentAnimTrack = idleAnimTrack
  idleAnimTrack:Play() elseif animType == "FallAnim" then
  currentAnimTrack = fallAnimTrack  fallAnimTrack:Play()
 endend)
-- مراقبة حالة الركض والمشي
humanoid.Running:Connect(function(speed) stopAllAnimations()  -- إيقاف الأنيميشنات السابقة

 if speed >= 18 then  local relativeSpeed = speed / 16
  playAnimation("Run", relativeSpeed, 1) elseif speed < 18 and speed > 0.5 then
  local relativeSpeed = speed / 16  playAnimation("Walk", relativeSpeed, 1)
 else  playAnimation("Idle", 1, 1)
 endend)
-- مراقبة حالة القفز والسقوط
humanoid.Jumping:Connect(function() playAnimation("Jump", 1, 1)
end)
humanoid.FreeFalling:Connect(function()
 playAnimation("Fall", 1, 1)end)
-- مراقبة الهبوط بعد السقوط
humanoid.StateChanged:Connect(function(_, newState) if newState == Enum.HumanoidStateType.Physics then
  playAnimation("Idle", 1, 1)
 endend)