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?
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?
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)
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.
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.
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
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.
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.