How do I stop the console from being spammed?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a system so the closer the player gets to a part different things happen
    I’ve already made that part of the script I just have an issue.

  2. What is the issue? Include screenshots / videos if possible!
    When I get into the distance where it will run different things it spams that chat with the code
    I need help trying to fix that issue. I know what debounces are but I don’t know how I would use them to fix this issue.

Here is my code all it does is detect if the player is within a certain distance from the part then it runs some code.

local soundsByDist = {
	First = {min = 57.1428571429, max = 42.8571428572};
	Second = {min = 42.8571428571, max = 28.5714285714};
}

local emitterPart = game.Workspace.Monster
local charRootPart = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart")

while wait() do
	local distance = (emitterPart.Position - charRootPart.Position).Magnitude
	for num, info in pairs(soundsByDist) do
		if distance >= info.max and distance < info.min and First = false then
			print(num)
		end 
	end
end

You should remove the

print(num)

If you dont want your output being spammed.

Yeah I’m using it to test the script. I’m actually gonna be using a sound to put in there and play and I don’t want it to spam also even if I put any code in there its gonna spam.

Considering you dont want your sound being spammed, you should try IsPlaying which is a property of sound.

if not sound.IsPlaying then
  sound:Play()
end

Add a wait

After print(num) add a wait() if you don’t want it spammed

You could also just change how often it is runned at the while wait() do change it to while wait(10) do and it will wait for 10 seconds before repeating.

You also could break and it will stop the loop

This is my script now

local soundsByDist = {
	First = {min = 57.1428571429, max = 42.8571428572, sound = script["Theme 1_1"]};
	Second = {min = 42.8571428571, max = 28.5714285714, sound = script["Theme 1_2"]};
}

local emitterPart = game.Workspace.Monster
local charRootPart = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart")

while wait() do
	local distance = (emitterPart.Position - charRootPart.Position).Magnitude
	for num, info in pairs(soundsByDist) do
		if distance >= info.max and distance < info.min then
			if not info.sound.IsPlaying then
				info.sound:Play()
			end
		end 
	end
end

It works and it plays the sound but now It doesn’t stop the previous sound if the player goes into the next distance. How would I fix this?

Yeah I dunno if that would work here because I’m playing sounds within those distances and if the player stays inside the distance for too long it will just play the sound again and they will overlap.

Do something like this.

		if distance >= info.max and distance < info.min then
			if not info.sound.IsPlaying then
				info.sound:Play()
			end
        else
               info.sound:Stop()
		end 
1 Like

Thank you its work fine now. I will mark your answer as the solution now

1 Like