I’m trying to make a dash system, but the first time i dash there is a delay with the animation, then it works as intended.
Server script
Remote.OnServerEvent:Connect(function(plr, dashType) --activate on server event
local char = plr.Character --get player character
local dashSound = char.DashScript.DashSound
local defaultWalkSpeed = char.Humanoid.WalkSpeed --get current humanoid walkspeed
local forwardDashAnimation = char:FindFirstChild("DashScript").Animation1 --get dash animation
local loadedForwardDashAnimation = char.Humanoid.Animator:LoadAnimation(forwardDashAnimation) --load animation to humanoid
if not char then return end -- Check if player's character exists
local humanoid = char:FindFirstChild("Humanoid")
if not humanoid then return end -- Check if humanoid exists
local moveDirection = humanoid.MoveDirection --get which direction the player is currently moving (x,y,z) axis
local dashDirection = nil -- variable to store the direction to dash
if moveDirection.magnitude > 0 then --if the player is currently moving
dashDirection = moveDirection * Vector3.new(1, 0, 1) -- Use character's movement direction if moving and remove y axis
else
dashDirection = -char.HumanoidRootPart.CFrame.lookVector * Vector3.new(1, 0, 1) -- Use the opposite charcter's look vector if not moving
end
if not table.find(db, plr.UserId) then -- if the player isn't in the cooldown table
table.insert(db, plr.UserId) -- insert the player in the cooldown table
if dashType == "W" then
loadedForwardDashAnimation:Play()
local smallerDashClone = smallerDash:Clone()
smallerDashClone.Parent = char:WaitForChild("HumanoidRootPart")
debris:AddItem(smallerDashClone, 0.8)
end
humanoid.WalkSpeed = 0
humanoid.AutoRotate = false
local slide = Instance.new("BodyVelocity", char.HumanoidRootPart) --create a bodyvelocity and parent it to HumanoidRootPart
slide.MaxForce = Vector3.new(1, 0, 1) * 30000 --set the bodyvelocity max force to allow for better control and higher speed
slide.Velocity = dashDirection.unit * 65 -- set the bodyvelocity velocity to the dashDirection with a magnitude of 80
dashSound:Play()
debris:AddItem(slide, 0.25)
wait(0.25)
humanoid.WalkSpeed = defaultWalkSpeed
humanoid.AutoRotate = true
wait(2.5)
for i, userId in ipairs(db) do --loop trough the cooldwon table
if userId == plr.UserId then -- if the userId in the cooldown table is equal to the player UserId
table.remove(db, i) -- remove the player's UserId from the table
break --break the loop because we don't need to continue looping if the player is no longer on cooldown
end
end
end
end)
its because the animation wasnt loaded the first time. you can fix this by preloading animations. another thing you can do is to cache the track you play
for example, i use a local script in replicated firt and play all animations on a dummy in order to preload them, like this
local M1s = game:GetService("ReplicatedFirst").Animations.Combat.M1s:GetChildren()
local combat = game:GetService("ReplicatedFirst").Animations.Combat:GetChildren()
local combat2 = game:GetService("ReplicatedFirst").Animations.Combat.Dodges:GetChildren()
local combat3 = game:GetService("ReplicatedFirst").Animations.Combat.Abilities:GetChildren()
for _,animation in pairs(M1s) do
if animation:IsA("Animation") then
if game.Workspace:FindFirstChild("PreloadRig") then
game.Workspace.PreloadRig:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(animation):Play()
print("loaded anim")
end
end
end
for _,animation in pairs(combat3) do
if animation:IsA("Animation") then
if game.Workspace:FindFirstChild("PreloadRig") then
game.Workspace.PreloadRig:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(animation):Play()
print("loaded anim")
end
end
end
for _,animation in pairs(combat2) do
if animation:IsA("Animation") then
if game.Workspace:FindFirstChild("PreloadRig") then
game.Workspace.PreloadRig:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(animation):Play()
print("loaded anim")
end
end
end
for _,animation in pairs(combat) do
if animation:IsA("Animation") then
if game.Workspace:FindFirstChild("PreloadRig") then
game.Workspace.PreloadRig:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(animation):Play()
print("loaded anim")
end
end
end
another thing i do is cache animation tracks when playing them server sided, like this
local animationCache = {}
local function cacheAnimation(player, animationName, animationTrack)
-- Check if the player already has an entry in the cache
if not animationCache[player] then
animationCache[player] = {} -- If not, create a new table for the player
end
-- Store the animation track in the player's cache table with the given name
animationCache[player][animationName] = animationTrack
end
-- Function to retrieve a cached animation for a player
local function getCachedAnimation(player, animationName)
-- Check if the player has any cached animations
if animationCache[player] then
-- Retrieve the animation track with the given name from the player's cache table
return animationCache[player][animationName]
end
return nil -- Return nil if no animation found for the player with the given name
end
bla bla bla
local cachedAnim = getCachedAnimation(plr, "name")
if cachedAnim then
cachedAnim:Play()
else
anim = pathtoanim
local animTrack = humanoid:LoadAnimation(anim)
animTrack:Play()
local name = "name"
cacheAnimation(plr, name, animTrack)
end