Stop The Function

i have made or tried chat function but i did while true do and i put wait(9) when the wait done the function will run again twice

coroutine.wrap(function()
		if roundType == "Banana" then
			local players = game.Players:GetChildren()
			for i = 1,#players do
				if players[i].Character ~= nil then
					players[i].Chatted:Connect(function(msg)
						if msg == "banana" then
							print("Im an epic banana")
						end
					end)
				end	
			end
		end
	end)()

like when the first function is functioning it will do

im an epic banana
and when the wait done
it will do
im an epic banana (2x)

btw here is the real script

coroutine.wrap(function()
		if roundType == "Banana" then
			local players = game.Players:GetChildren()
			for i = 1,#players do
				if players[i].Character ~= nil then
					players[i].Chatted:Connect(function(msg)
						if msg == "banana" then
							print("Im an epic banana")
						end
					end)
				end	
			end
		end
	end)()
while true do
wait()
local players = game.Players:GetChildren()
			for i = 1,#players do
				if players[i].Character ~= nil then
					players[i].Chatted:Connect(function(msg)
						if msg == "banana" then
							print("Im an epic banana")
                                                         wait(9)
if thing == thing then
break
end
						end
					end)
				end	
			end
                 end

I’m so confused, what are you trying to accomplish?

Also don’t make connections in a while loop like that it will run the code passed to :Connect every time it’s fired as many times as the while loop created a connection. (sorry for bad explanation, basically if the while loop connects .Chatted 10 times, every time the player chats the function will run 10 times)

sorry thats not the real script
i re edit the script

here is the full script

local timer = game.ReplicatedStorage.Value.Timer

local maps = game.ReplicatedStorage.Maps:GetChildren()

while true do
	for i = 1,10 do
		timer.Value = "".. 10-i
		wait(1)
	end
	
	local rand = math.random(1,#maps)
	
	local map = maps[rand]:Clone()
	map.Parent = game.Workspace.Map
	
	local players = game.Players:GetChildren()
	for i = 1,#players do
		if players[i].Character ~= nil then
			local teleporter = math.random(1,#map.Spawn:GetChildren())
			players[i].Character:MoveTo(map.Spawn:GetChildren()[teleporter].Position)
			players[i].Character.Parent = workspace.Ingame
		end
	end	
	
	local roundLength = 50
	local roundType = ""
	
	
	if map:FindFirstChild("Banana") then
		roundType = "Banana"
		roundLength = roundLength + 30
	elseif map:FindFirstChild("Mango") then
		roundType = "Mango"
		roundLength = roundLength + 10
		
	end
	
	coroutine.wrap(function()
		if roundType == "Banana" then
			local players = game.Players:GetChildren()
			for i = 1,#players do
				if players[i].Character ~= nil then
					players[i].Chatted:Connect(function(msg)
						if msg == "banana" then
							print("Im an epic banana")
						end
					end)
				end	
			end
		end
	end)()
	
	coroutine.wrap(function()
		if roundType == "Mango" then
			local players = game.Players:GetChildren()
			for i = 1,#players do
				if players[i].Character ~= nil then
					players[i].Chatted:Connect(function(msg)
						if msg == "mango" then
							print("Im an epic mango")
						end
					end)
				end	
			end
		end
	end)()
	
	repeat
		roundLength -= 1
		timer.Value = ""..roundLength
		wait(1)
	until
	roundLength == 0 and roundType == "Banana" or
		#workspace.Ingame:GetChildren() == 0 and roundType == "Banana" or -- Banana
----------------------------------------------------------------------------------------------
	roundLength == 0 and roundType == "Mango" or
		#workspace.Ingame:GetChildren() == 0 and roundType == "Mango" -- Mango
	
	wait()
	roundType = ""
	local players = game.Players:GetChildren()
	for i = 1,#players do
		if players[i].Character ~= nil then
			players[i]:LoadCharacter()
			map:Destroy()
		end
	end	
end

what are you trying to achieve??

If you want something to happen when any player chats you do

local function onChatted (message)
     print(message)
end

for _,player in ipairs(game.Players:GetPlayer())do
   player.Chatted:Connect(onChatted)
end

game.Players.PlayerAdded:Connect(onChatted)

You still didn’t tell me what you are trying to do, again I wouldn’t make connections in a while loop in the full code you posted.

i wanna stop chat event cuz its still working when the round is done also
it will times two when the other round is functioning

yes that what I was saying, don’t put connections in a while loop because then they will stack

You can use Connection:Disconnect() to disconnect a function

debounce
use debounce by doing this

loca db = false
if not db then
db = true
somthing
end

i did not put connection in the script

That won’t help them at all, they are trying to disconnect the .Chatted event.

dont think its possible
at all debounce could do it doe

local connections = {}
coroutine.wrap(function()
		if roundType == "Banana" then
			local players = game.Players:GetChildren()
			for i = 1,#players do
				if players[i].Character ~= nil then
					connection[player] = players[i].Chatted:Connect(function(msg)
						if msg == "banana" then
							print("Im an epic banana")
						end
					end)
				end	
			end
		end
	end)()

------
-- at the end of your while loop

for _,con in pairs(connections)do
   con:Disconnect()
end

my guy I don’t know what you are talking about, I don’t think you understand the problem here.

but it is still in the while loop imma try it

i clearly do understand the problem ok?

use debounce to temporarey (sorry) to break the while loop

Yes then explain to me what the problem is and what the solution is, a debounce is irrelevant in this scenario and we are going off topic.


@sausagerate try the code I posted the lines I added were

the problem is of disconnecting the function and breaking the while loop
duh