Need help with an emote player bug

I made an emote player. It plays the animations correctly, but for some reason it doesn’t stop. Does anyone know why?

Here’s the code:

local S_PLAYERS = game:GetService("Players")
local S_RS = game:GetService("ReplicatedStorage")

--// VARIABLES
local PLAYER = S_PLAYERS.LocalPlayer

repeat wait() until PLAYER.Character

local CHARACTER = PLAYER.Character
local HUMANOID = CHARACTER:WaitForChild("Humanoid")

local ANIMATIONS = CHARACTER:WaitForChild("ANIMATIONS")

local MODULE_SCRIPTS = S_RS:WaitForChild("MODULE_SCRIPTS")

local EMOTE_DATA = require(MODULE_SCRIPTS.EMOTE_DATA)

local CURRENT_EMOTE = nil

--[PUBLIC]--

--// Returns all of the emotes in the module.
local function return_all_emotes()
	local emotes_table = {}
	for _, emote in EMOTE_DATA do
		if emote then
			table.insert(emotes_table, emote)
		end
	end
	return emotes_table
end

--// Adds animation instances.
local function add_animation_instances()
	local emote_table = return_all_emotes()
	for _, emote in pairs(emote_table) do
		if emote then
			local animation_instance = Instance.new("Animation")
			animation_instance.Name = emote.Name
			animation_instance.AnimationId = emote.Animation_ID
			animation_instance.Parent = ANIMATIONS
		end
	end
end

--// Loads the animation.
local function load_animation(animation)
	local animator = HUMANOID:FindFirstChild("Animator") or Instance.new("Animator", HUMANOID)
	if animator then
		for _, track in animator:GetPlayingAnimationTracks() do
			if track then
				return track
			end
		end
		local anim_track = animator:LoadAnimation(animation)
		return anim_track
	end
end

--// The main emote button.
local function emote_button(button : TextButton)
	button.MouseButton1Down:Connect(function()
		if CURRENT_EMOTE == nil then
			local animator = HUMANOID:FindFirstChild("Animator") or Instance.new("Animator", HUMANOID)
			if animator then
				CURRENT_EMOTE = animator:LoadAnimation(ANIMATIONS:WaitForChild(button.Name))
				CURRENT_EMOTE:Play()
			end
		elseif CURRENT_EMOTE ~= nil then
			if CURRENT_EMOTE == button.Name then
				CURRENT_EMOTE:Stop()
				--CURRENT_EMOTE = nil
			elseif CURRENT_EMOTE ~= button.Name then
				CURRENT_EMOTE:Stop()
				--CURRENT_EMOTE = nil
				local animator = HUMANOID:FindFirstChild("Animator") or Instance.new("Animator", HUMANOID)
				if animator then
					CURRENT_EMOTE = animator:LoadAnimation(ANIMATIONS:WaitForChild(button.Name))
					CURRENT_EMOTE:Play()
				end
			end
		end
	end)
end

--[MAIN]--

for _, emote in return_all_emotes() do
	if emote then
		local button = script:WaitForChild("EMOTE_PLACEHOLDER"):Clone()
		button.Name = emote.Name
		button.Text = emote.Name
		button.Parent = script.Parent:WaitForChild("EMOTES_HOLDER"):WaitForChild("SCROLLING_FRAME")
	end
end

for _, button in script.Parent:WaitForChild("EMOTES_HOLDER"):WaitForChild("SCROLLING_FRAME"):GetChildren() do
	if button then
		if button:IsA("TextButton") then
			emote_button(button)
			task.wait(0.1)
		end
	end
end

script.Parent:WaitForChild("EMOTES").Activated:Connect(function()
	script.Parent:WaitForChild("EMOTES_HOLDER").Visible = not script.Parent:WaitForChild("EMOTES_HOLDER").Visible
end)

while task.wait() do
	if #ANIMATIONS:GetChildren() == 0 then
		add_animation_instances()
	end
end

The module script only stores the name of the animation, and the animation ID.

1 Like
if CURRENT_EMOTE.Animation.Name == button.Name then
	CURRENT_EMOTE:Stop()
	--CURRENT_EMOTE = nil
elseif CURRENT_EMOTE.Animation.Name ~= button.Name then
	CURRENT_EMOTE:Stop()
	--CURRENT_EMOTE = nil
	local animator = HUMANOID:FindFirstChild("Animator") or Instance.new("Animator", HUMANOID)
	if animator then
		CURRENT_EMOTE = animator:LoadAnimation(ANIMATIONS:WaitForChild(button.Name))
		CURRENT_EMOTE:Play()
	end
end

@Azurethpat In case you were wondering, you were initially comparing an Instance == string, which returned false ofc. My current code compares the name of the animation of the current track (string) == button.Name (string)and they will be equal since your animations are named the same as your buttons.

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