Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i make it where when you run it plays a sound but when you stop moving it would stop playing?

Asked by 5 years ago
Edited 5 years ago

So i got a run script that displays a run animation and i dont know how to make it play a sound. how do i make it play the sound when you run? what i got so far is ~~~~~~~~~~~~~~~~~

local Player = game.Players.LocalPlayer
    local Character = workspace:WaitForChild(Player.Name)
        local Humanoid = Character:WaitForChild('Humanoid')

local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://2543615157'
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
    if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
        Running = true
        Humanoid.WalkSpeed = 70
    elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then
        Running = false
        if RAnimation.IsPlaying then
            RAnimation:Stop()
        end
        Humanoid.WalkSpeed = 16
        sound:Stop()
    end
end

Humanoid.Running:connect(function(Speed)
    if Speed >= 10 and Running and not RAnimation.IsPlaying then
        RAnimation:Play()
        Humanoid.WalkSpeed = 70
        sound:Play()
    elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
        sound:Stop()
    elseif Speed < 10 and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
        sound:Stop()
    end
end)

Humanoid.Changed:connect(function()
    if Humanoid.Jump and RAnimation.IsPlaying then
        RAnimation:Stop()
    end
end)

game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.LeftShift)

~~~~~~~~~~~~~~~~~

0
format please DeceptiveCaster 3761 — 5y
0
capitalize connect() DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hi superpichufan_57,

You might want to try using GetPropertyChangedSignal("MoveDirection")

With this you can check how fast the character is moving and act accordingly

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
    if humanoid.MoveDirection.Magnitude > 0 then -- if they are moving
        if sound.IsPlaying == false then -- if it isn't already playing
            sound:Play()
        end
    else
        sound:Stop()
    end
end)

Of course you could tailor this to your specific situation. Let me know if this helps.

0
it didnt work superpichufan_57 0 — 5y
0
what was the error? Also, plugging this exact code into your script won't work, you will have to modify it to your own variables. PoePoeCannon 519 — 5y
Ad

Answer this question