Sound does not stop when i want it to

Hi there,

I could use help on this problem i got.
I want the sound to stop but it doesn’t can anyone tell me another way then using runservice for this.
I know im using runservice and renderstepped but i don’t know any other way. Please give me some advice or help

Script:

runservice.RenderStepped:Connect(function()
	if humanoid.MoveDirection.Magnitude > 0 then
		script.walkingsound.Playing = true
		script.walkingsound.Pitch = 0.72
	else
		script.walkingsound.Playing = false
	end

end)


-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then	
			tweenFOV(0.5, 85)
			tweenWalkspeed(0.5, 30)
			script.walkingsound:Stop()
			script.walkingsound.Looped = false
			script.SprintingSound.Playing = true
			script.SprintingSound.Looped = true
			script.SprintingSound.Pitch = 1.72
		end
	end
end)

Have you tried using Sound:Stop() instead of Sound.Playing = false ?

Man you need to use code tags, all you have to do is click on the little button that looks like this </>. It’s easy, then paste your code in. I’m not even going to try replying unless you take the effort to do this.

Sorry man i didn’t know how to do it

1 Like

Cool, I passed something important on to you. That at least is something…

This is bad practice, you assign this in two different places in events that fire very frequently. It may be better to assign a boolean to true when you do play the sound then you check whether this is true before you go to play the sound again.

Chill dude… It was one tiny detail…

Okay that helps everyone here, especially the OP. Why not post a solution instead?

Try this script parented under StarterCharacterScripts:

local RunService = game:GetService("RunService");
local UserInputService = game:GetService("UserInputService");

--=================================================================================
local Humanoid = script.Parent:WaitForChild("Humanoid");
local Camera = game.Workspace.CurrentCamera;
--=================================================================================
local currentStamina = 100;
local staminaCost = 100;
local walkSound = script:WaitForChild("WalkingSound",3);
walkSound.Looped = false;
walkSound.Pitch = 0.72;
local sprintSound = script:WaitForChild("SprintingSound",3);
sprintSound.Looped = true;
sprintSound.Pitch = 1.72;
local movementMagnitude = 0;
local shiftDown = false;

--=================================================================================
RunService.RenderStepped:Connect(function()
	
	movementMagnitude = Humanoid.MoveDirection.Magnitude;
	if movementMagnitude > 0 and Humanoid:GetState() == Enum.HumanoidStateType.Running then -- movement
		if shiftDown == true then -- sprinting?
			if sprintSound.IsPlaying == false then
				sprintSound:Play();
				walkSound:Stop();
			end
			
		else
			if walkSound.IsPlaying == false then
				walkSound:Play();
				sprintSound:Stop();
			end
			
		end
	else	-- no movement
		if shiftDown == true then
			sprintSound:Stop();
		else
			walkSound:Stop(); 
		end
	end
	
end)

--=================================================================================
-- Sprint Key Pressed
--=================================================================================
UserInputService.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		
		if currentStamina >= staminaCost and movementMagnitude > 0 then
			
			Humanoid.WalkSpeed = 30;
			Camera.FieldOfView = 85;
			shiftDown = true;

		end
		
	end
	
end)
--=================================================================================
-- Sprint Key De-Pressed
--=================================================================================
UserInputService.InputEnded:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.LeftShift then
		
		Humanoid.WalkSpeed = 16;
		Camera.FieldOfView = 70;
		shiftDown = false;
		
	end
	
end)

--=================================================================================
-- EOF...
--=================================================================================