Люди как сделать так чтобы во время проигрывания 1 анимации при запуске другой первая не перекрывала 2?
local player = game.Players.LocalPlayer
local currentAnim = nil -- Хранит текущую анимацию
local isPlaying = false -- Проверяет, играет ли анимация
local anim_id = "rbxassetid://1234567890"
local cooldown = 1.3 -- Задержка перед повторным запуском
script.Parent.MouseButton1Click:Connect(function()
local character = player.Character
if not character then return end
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if not humanoid then return end
local animator = humanoid:FindFirstChildWhichIsA("Animator")
if not animator then return end
if not isPlaying then
isPlaying = true
-- Если уже есть активная анимация, останавливаем её
if currentAnim then
currentAnim:Stop()
currentAnim = nil
end
-- Создаём и запускаем новую анимацию
local newAnimation = Instance.new("Animation")
newAnimation.AnimationId = anim_id
currentAnim = animator:LoadAnimation(newAnimation)
currentAnim.Looped = true
currentAnim:Play()
task.wait(cooldown) -- Задержка перед возможностью повторного нажатия
isPlaying = false
end
end)
что тут нужно исправить чтобы всё нормально работало? (код на языке lua для роблокс студио)
короче тут фишка в том, что currentAnim:Stop() может не срабатывать мгновенно, и если анимации накладываются, значит она не успевает завершиться до начала следующей. ещё может быть, что анимация в Animator не полностью сбрасывается.
вот пофикшенный и чуть почище вариант:
local player = game.Players.LocalPlayer
local currentAnimTrack = nil
local animCooldown = 1.3
local animId = "rbxassetid://1234567890"
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if debounce then return end
debounce = true
local character = player.Character
if not character then debounce = false return end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then debounce = false return end
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then debounce = false return end
-- останавливаем предыдущую анимацию если была
if currentAnimTrack then
currentAnimTrack:Stop(0.1) -- плавная остановка
currentAnimTrack:Destroy() -- гарантированное удаление
currentAnimTrack = nil
end
local anim = Instance.new("Animation")
anim.AnimationId = animId
local newTrack = animator:LoadAnimation(anim)
newTrack.Looped = true
newTrack:Play()
currentAnimTrack = newTrack
task.delay(animCooldown, function()
debounce = false
end)
end)
добавил Destroy() после Stop(), чтобы точно убрать трек и он не мешался
заменил FindFirstChildWhichIsA на FindFirstChildOfClass — оно чётче работает
debounce вместо isPlaying, чтобы проще было отсечь спам
теперь всё должно запускаться плавно и предыдущая анимация не будет мешать новой.