Lately, I’ve been working on a Emote Gamepass system for my game. But for some reason when the animation plays, it also plays the animation on the Server. Furthermore, the player has to be moving on the Server instead of the Client to stop the current animation using Connection. I’ve tried researching this issue but to no avail.
Client:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EmoteData = require(ReplicatedStorage.Modules:WaitForChild("EmotesModule"))
local animEvent = ReplicatedStorage.Events.Remotes:WaitForChild("PlayAnimationEvent")
local player = Players.LocalPlayer
local activeAnimation
local function playAnimation(emote)
if activeAnimation then
activeAnimation:Stop()
end
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
local rootPart = character:FindFirstChild("HumanoidRootPart")
if humanoid and rootPart then
local animation = Instance.new("Animation")
animation.AnimationId = EmoteData.Animations[emote]
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()
activeAnimation = animationTrack
local currentPos = rootPart.Position
local connection
connection = rootPart:GetPropertyChangedSignal("Position"):Connect(function()
if (rootPart.Position - currentPos).Magnitude > 0.1 then
animationTrack:Stop()
connection:Disconnect()
end
end)
animationTrack.Stopped:Connect(function()
animation:Destroy()
activeAnimation = nil
end)
end
end
end
local function playSound(emote)
if EmoteData.Sounds[emote] then
local character = player.Character
if character then
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
local sound = humanoidRootPart:FindFirstChild("EmoteSFX")
if not sound then
sound = Instance.new("Sound", humanoidRootPart)
sound.Name = "EmoteSFX"
sound.SoundId = EmoteData.Sounds[emote]
end
sound:Play()
end
end
end
end
animEvent.OnClientEvent:Connect(function(requestingPlayer, emote)
if not emote or requestingPlayer ~= player then return end
playAnimation(emote)
playSound(emote)
end)
Server:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")
local EmoteData = require(ReplicatedStorage.Modules:WaitForChild("EmotesModule"))
local animEvent = ReplicatedStorage.Events.Remotes:WaitForChild("PlayAnimationEvent")
local gamepassId = 242917287
local function onEmoteRequested(player, emote)
local success, hasPass = pcall(function()
return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
end)
if success and hasPass and EmoteData.Animations[emote] then
animEvent:FireAllClients(player, emote)
end
end
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if string.lower(msg) then
msg = string.gsub(msg, "/e ", "")
onEmoteRequested(player, msg)
end
end)
end)