I’m trying to create emotes for my game, but I’m not entirely sure how to do it. Right now, I am trying to set it up so that, when a button is pressed in a GUI, a local script picks it up and sends it through a remote event to a server script, which then applies the animation. The problem is I don’t know how to make it so that that animation stops playing without referring to a different animation by accident, which is what (I assume) keeps happening each time I try to make this work.
My main question is how do I get this to play when the button is pressed once, and to stop that animation once pressed again?
Local script:
local EmotesFrame = script.Parent
local innerFrame = EmotesFrame.InnerFrame
local Input = game:GetService("UserInputService")
local module = require(game.ReplicatedStorage.Variables)
local player = game.Players.LocalPlayer
local character = player.Character
local animated = false
innerFrame.Back.MouseButton1Click:Connect(function()
EmotesFrame.Visible = false
end)
innerFrame.Lay.MouseButton1Click:Connect(function()
if animated == false then
local human = character:WaitForChild("Humanoid")
local anim = innerFrame.Lay.Animation
human.WalkSpeed = 0
game.ReplicatedStorage.Animation:FireServer(anim, animated)
animated = true
else
local human = character:WaitForChild("Humanoid")
local anim = innerFrame.Lay.Animation
human.WalkSpeed = 16
game.ReplicatedStorage.Animation:FireServer(anim, animated)
animated = false
end
end)
Server script:
game.ReplicatedStorage.Animation.OnServerEvent:Connect(function(player, animation, animated)
local anim
if animated == false then
local character = player.Character
local human = character:WaitForChild("Humanoid")
anim = human:LoadAnimation(animation)
anim:Play()
else
anim:Stop()
anim:Remove()
end
end)
If you just put the first and second script inside a server script with the same parent as the local script then everything should work so then you dont have to use remoteEvents but i also think you can just do the same in a local script too and ir will work
Putting it all into one local script would work and be more efficient (Drawback beings the animation only plays for ClientSide) but if you really want to keep the two separate scripts then you could use two for i loops to check all playing animations and see if their names are equal to your emotes names. If they are then you can use :Stop on them. (Sorry if the code has an error or two I typed this out on my phone, so be careful and use it as an example)
— Example (Put this where you stop the animations)
local Character = player.Character
local EmotesFolder = — put your folder containing the emotes here.
local Tracks = Character.Humanoid:GetPlayingAnimationTracks() — We’ll use this to get every animation playing on the player right now.
for i,v in pairs(EmotesFolder:GetChildren() do
for i,X in pairs(Tracks) do
if v.Name == X.Name then
X:Stop()
end
end
end
They can be accessed by both, I’m trying to make it so the emotes can be seen by everyone for whoever clicks it. It also doesn’t need to be two scripts, I was just assuming that’s the only way since it needs to detect a click of the button which I assumed can only be done on a local script but if there’s other ways that’s better then I’d much rather do that.
I also want to clarify, the main problem with the script is that it will PLAY, but it won’t STOP playing when I press it again. I printed out the variables as strings and all of them are normal, but ‘animation’ just prints out as “Animation” which IS what the animation is called, so I’m not sure if printing paths as a string just prints the name and not the path or if that’s what’s broken. Also, this is the error I get which I forgot to include:
The problem is that you have to stop the track itself not the animation. Your script is sending the animation over to the server script which you can’t use :Stop() on. You’d have to find a way to get the track you used and :Stop() that.
The solution here would be to use something close to what I had sent before. Where you find the track already playing on the character and use :Stop() on that.