How do i recreate the /e emote system?

i’ve been trying to get a script to work where an animation would play when you type /e [name], and the animation is easy enough but I also want to add audio. The system is a server script which is similar to this:

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		value = Instance.new("BoolValue") --value to check for player movement
		value.Name = "moving"
		value.Parent = c
			while true do
				local a = c.HumanoidRootPart.Position
				task.wait(0.1)
				local b = c.HumanoidRootPart.Position
				if (a - b).Magnitude > 0.1 then
					value.Value = true
				else
					value.Value = false
				end
			end
		end)
	p.Chatted:Connect(function(c)
		local char = p.Character or p.CharacterAdded:Wait()
		if c == prefix .. " " .. emote1.Name and char.Humanoid.Sit ~= true then --checks if player can emote
			local a = Instance.new("Animation")
			a.AnimationId = "rbxassetid://" .. emote1.AnimId
			local anim = char.Humanoid:LoadAnimation(a)
			anim:Play()
			local s = Instance.new("Sound")
			s.SoundId = "rbxassetid://" .. emote1.SoundId
			s.Parent = char.HumanoidRootPart
			s.Playing = true
			s.Looped = true
			while task.wait() do --while value false loop on the value itself did not work for some reason, had to go with a constant check
				if s and value.Value == true then
					
					s:Destroy() -- removes all animations and sounds
					anim:Stop()
					anim:Destroy()
				end
			end
		end
	end)
end)

The issue is that even when another player moves, the music stops even though the emoting character has not moved yet. How would i solve this issue?

5 Likes

why not check HumanoidRootPart.Velocity.Magnitude to check if its moving (> 0.1 moving, < 0.1 idle), maybe thats why?

5 Likes

i did but it didn’t work. i can try again and check for the error because i forgot it if you want.

1 Like

that is also not the issue as the issue is that the emote music stops playing as soon as another player walks, even if they are not emoting.

1 Like

OP, use humanoid.MoveDirection.Magnitude instead of hrp.Velocity…

If MoveDirection.Magnitude is 0 then the player is not moving. If it is greater than 0 the player is moving.

1 Like

no i need to check if the player moves at all, even when involuntary.

1 Like

Inside of this loop, you are getting the HRP’s new position twice. When the dance starts, get its position and then keep checking if it changes.

while true do
	local a = c.HumanoidRootPart.Position
	task.wait(0.1)
	local b = c.HumanoidRootPart.Position
	if (a - b).Magnitude > 0.1 then
		value.Value = true
	else
		value.Value = false
	end
end

Alternatively you can create a connection for a CFrameValue inside of the char that updates every heartbeat.

the loop is supposed to compare the position 0.1 seconds away from each other and check if any movement has happened. Can you provide some pseudo code as an example of how i can do your suggested method of detecting movement?

Something similar to this should work for you.

local players = game:GetService("Players")
local run = game:GetService("RunService")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		task.wait(.75)
		local hrp = char.HumanoidRootPart
		local spawnPos = hrp.CFrame.Position
		
		local function checkIfLeftSpawn()
			local newPos = hrp.CFrame.Position
			if (spawnPos - newPos).Magnitude > 0 then
				print("Character has left spawn")
			end
		end

		run.Heartbeat:Connect(checkIfLeftSpawn)
	end)	
end)

thanks for the code, I will take note of this.
Do you have any info on my problem of the emote music issue?

I have a couple tips, which might fix your issue. If all of these checks happen in the same connection i believe that another player wont be able to do anything to the sound.

  1. Don’t use the else and set the value to false in the while true loop, instead set the value to false outside when the event is fired.

  2. To check the for the emote command in chat, don’t use prefix. Instead use…

if c:sub(1, emote1.Name:len()):lower() == emote1.Name:lower() then
    -- Your code here.
end
  1. If you are creating multiple emotes, then check for sitting first and then check for each emote inside of that if statement. Should make it a bit easier to read.

  2. The movement check should not run constantly, only when you start an emote. Start checking after the animation and sound are made… If you do it this way you shouldn’t need the BoolValue anymore.