Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

format please

0

capitalize connect()

1 answer

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.

2 comments

0

it didnt work

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.