Emote Animations Overlapping

Hello! I’m trying to make an emote gui for my game. I put a ScrollingFrame with TextButtons inside, and inside each button is a local script and an animation. The problem is, when I test the game and I press a button, the animation does play, but after I press another button the animations overlap with eachother. How can I fix this? By the way here is the local script inside each button:

local player = game.Players.LocalPlayer
local chr = player.Character
local playing = false
local hum = chr:WaitForChild("Humanoid")
local animation = script:FindFirstChild("Animation")
local btn = script.Parent
local lod = hum:LoadAnimation(animation)

btn.MouseButton1Click:Connect(function()
	if playing == false then
		playing = true
		lod:Play()
	else
		playing = false
		lod:Stop()
	end
end)

Combine all the scripts into one script. We can easily do this With a for loop. Here is an example:

local ScrollingFrame = script.Parent --Change this to the pathway of your button
local CurrentlyPlaying = nil
local LoadedAnimation = nil

local player = game.Players.LocalPlayer
local chr = player.Character
local hum = chr:WaitForChild("Humanoid")


for i, Button in ScrollingFrame:GetChildren() do 
  if Button:IsA("TextButton") and Button:FindFirstChildWhichIsA("Animation") then
     Button.MouseButton1Click:Connect(function()
      if CurrentlyPlaying == nil then 
       CurrentlyPlaying = Button:FindFirstChildWhichIsA("Animation")
        LoadedAnimation = hum:FindFirstChildWhichIsA("Animator"):LoadAnimation(CurrentlyPlaying)
        LoadedAnimation:Play()
     elseif CurrentlyPlaying == Button:FindFirstChildWhichIsA("Animation") then 
       LoadedAnimation:Stop()
       LoadedAnimation = nil
       CurrentlyPlaying = nil
    else 
        LoadedAnimation:Stop()
        CurrentlyPlaying = Button:FindFirstChildWhichIsA("Animation")
        LoadedAnimation = hum:FindFirstChildWhichIsA("Animator"):LoadAnimation(CurrentlyPlaying)
        LoadedAnimation:Play()
      end
   end
end

There should only be one of these scripts, remove/disable all your local scripts for this emote system. Each Text button should have a Animation as a child with the Proper animation for each button

Hope this helps! Let me know if you have questions!

You’re a life saver, thank you so much!!

1 Like

Also sorry for the late reply, but my game also has custom characters, they have a humanoid with animator and everything but the emotes just don’t seem to work on them. Do you have any idea why?
(edit: I managed to figure it out, all I had to do was set the gui to reset on spawn)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.