Emote Overlap Problem

hi, im currently working on an emote/dance system and i have a small problem where two emotes and their songs overlap.

all i want is that the currently playing emote and its respective music is stopped when another one starts playing.

i would try to figure it out on my own but i genuinely dont know where to start, any amount of help would be and is greatly appreciated. (i tried a couple times before but none of those attempts were successful)

heres a video of the problem at hand: (animations and music are placeholders)

heres an image of the way the emote GUI is setup:
Screenshot 2023-07-27 202410

script located in each of the TextButtons:

local repStorage = game:GetService("ReplicatedStorage")
local Icon = require(repStorage.Icon)

local players = game:GetService("Players")
local lPlayer = players.LocalPlayer
local char = lPlayer.Character
local humanoid = char:WaitForChild("Humanoid")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://14206950747"
local trackEmote = humanoid:LoadAnimation(animation)

local button = script.Parent

local canPlay = true
local playingValue = script.Parent.Parent.Music.Value


local function playAnimation(animationSource)
	if canPlay == true and playingValue == false then
		trackEmote:Play()
		char:WaitForChild("HumanoidRootPart").Anchored = true
		local music = script.Parent.Music:Clone()
		music.Name = "Music"
		music.Parent = char:WaitForChild("HumanoidRootPart")
		music.Playing = true
		music.Looped = true
		
		canPlay = false
		playingValue = true
		
		button.BackgroundColor3 = Color3.fromRGB(255,255,255)
		button.Text = "Stop"
		button.TextColor3 = Color3.fromRGB(0,0,0)
		button.TextStrokeTransparency = 1
	elseif canPlay == false and playingValue == true then
		trackEmote:Stop()
		char:WaitForChild("HumanoidRootPart").Anchored = false
		char:WaitForChild("HumanoidRootPart"):WaitForChild("Music"):Destroy()
		script.Parent.Parent.Music.Value = false
		
		canPlay = true
		playingValue = false
		
		button.BackgroundColor3 = Color3.fromRGB(0,0,0)
		button.Text = "Kazotsky Kick"
		button.TextColor3 = Color3.fromRGB(255,255,255)
		button.TextStrokeTransparency = 0
	end
end

script.Parent.MouseButton1Click:Connect(playAnimation)

script for topbarplus located in starterPlayerScripts: (topbarplus module is located in ReplicatedStorage)

local repStorage = game:GetService("ReplicatedStorage")

local Icon = require(repStorage.Icon)

local players = game:GetService("Players")
local localPlayer = players.LocalPlayer

local EmoteGUI = localPlayer:WaitForChild("PlayerGui"):WaitForChild("EmoteGUI"):WaitForChild("Frame")

-- TopBarPlus crap
local icon = Icon.new()
icon:setImage("rbxassetid://14206317093")
icon:setLabel("Emotes", "hovering")

icon:bindToggleKey(Enum.KeyCode.G)
icon:bindToggleItem(EmoteGUI)

Instead of making a script in each TextButton, you should make just one script to control all of them. I made a script that will work for it, you just need to include a number attribute in each button named “AnimationId” and insert the correct value.

Make a new LocalScript under ScrollingFrame and paste this inside:

local repStorage = game:GetService("ReplicatedStorage")
local Icon = require(repStorage.Icon)

local players = game:GetService("Players")
local lPlayer = players.LocalPlayer
local char = lPlayer.Character
local humanoid = char:WaitForChild("Humanoid")

--//Variables
local ScrollingFrame = script.Parent
local playingValue = ScrollingFrame.Music

--//Controls
local currentEmote = nil

--//Functions
local function InitializeButton(button)
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://".. button:GetAttribute("AnimationId")

	local trackEmote = humanoid:LoadAnimation(animation)

	local originalText = button.Text

	local function playAnimation()
		if currentEmote == trackEmote then
			trackEmote:Stop()
			char:WaitForChild("HumanoidRootPart").Anchored = false
			char:WaitForChild("HumanoidRootPart"):WaitForChild("Music"):Destroy()

			button.BackgroundColor3 = Color3.fromRGB(0,0,0)
			button.Text = originalText
			button.TextColor3 = Color3.fromRGB(255,255,255)
			button.TextStrokeTransparency = 0

			return
		end

		if currentEmote then
			currentEmote:Stop()

			char:WaitForChild("HumanoidRootPart"):WaitForChild("Music"):Destroy()
		end

		char:WaitForChild("HumanoidRootPart").Anchored = true

		trackEmote:Play()
		currentEmote = trackEmote

		local music = script.Parent.Music:Clone()
		music.Name = "Music"
		music.Parent = char:WaitForChild("HumanoidRootPart")
		music.Playing = true
		music.Looped = true

		button.BackgroundColor3 = Color3.fromRGB(255,255,255)
		button.Text = "Stop"
		button.TextColor3 = Color3.fromRGB(0,0,0)
		button.TextStrokeTransparency = 1
	end

	button.MouseButton1Click:Connect(playAnimation)
end

for i, child in ipairs(ScrollingFrame:GetChildren()) do
	if child:IsA("GuiButton") then
		task.spawn(InitializeButton, child)
	end
end
1 Like

it appears to work, uhhmm… kind of?

i had to edit some tiny stuff in the script to get it to play animations and music but it appears that the buttons break pretty badly,

What are the errors? Also, try this:

local repStorage = game:GetService("ReplicatedStorage")
local Icon = require(repStorage.Icon)

local players = game:GetService("Players")
local lPlayer = players.LocalPlayer
local char = lPlayer.Character
local humanoid = char:WaitForChild("Humanoid")

--//Variables
local ScrollingFrame = script.Parent
local playingValue = ScrollingFrame.Music

--//Controls
local currentEmote = nil

--//Functions
local function InitializeButton(button)
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://".. button:GetAttribute("AnimationId")

	local trackEmote = humanoid:LoadAnimation(animation)

	local originalText = button.Text

	local function playAnimation()
		if currentEmote == trackEmote then
			currentEmote:Stop()
			currentEmote = nil
			
			char:WaitForChild("HumanoidRootPart").Anchored = false
			char:WaitForChild("HumanoidRootPart"):WaitForChild("Music"):Destroy()

			button.BackgroundColor3 = Color3.fromRGB(0,0,0)
			button.Text = originalText
			button.TextColor3 = Color3.fromRGB(255,255,255)
			button.TextStrokeTransparency = 0

			return
		end

		if currentEmote then
			currentEmote:Stop()
			currentEmote = nil

			char:WaitForChild("HumanoidRootPart"):WaitForChild("Music"):Destroy()
		end

		char:WaitForChild("HumanoidRootPart").Anchored = true

		trackEmote:Play()
		currentEmote = trackEmote

		local music = button.Music:Clone()
		music.Name = "Music"
		music.Parent = char:WaitForChild("HumanoidRootPart")
		music.Playing = true
		music.Looped = true

		button.BackgroundColor3 = Color3.fromRGB(255,255,255)
		button.Text = "Stop"
		button.TextColor3 = Color3.fromRGB(0,0,0)
		button.TextStrokeTransparency = 1
	end

	button.MouseButton1Click:Connect(playAnimation)
end

for i, child in ipairs(ScrollingFrame:GetChildren()) do
	if child:IsA("GuiButton") then
		task.spawn(InitializeButton, child)
	end
end
1 Like

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