Stop The Function

No @sausagerate never said they want to break the while loop.

Also which function are you talking about should be disconnected?

its not still working it didnt stop the function

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
					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()
			for _,con in pairs(connections)do
				con:Disconnect()
			end
		end
	end	
end

take out the coroutine, there is no need for a coroutine

i need to cuz when i dont put courotin the script will break

why will the script break could you elaborate? In fact the coroutine should be removed so ensure the .Chatted functions are connected before running the next part of the code


@Qinrir okay I was just trying to explain to you the problem and why you werent giving a solution to the problem

cuz i put this

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

and so when the roundtype is banana it need to fuction in other way
so i make this

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)()

also cuz i cant put the fuction on main if map: etc

Excuse me can you please tell me what you are trying to make because i dont know what your trying to make yet if u tell me i can help then :smiley:

I thought you knew everything.


@sausagerate I don’t get it there should not be a coroutine and nothing will break the if statement will work fine

i wanna stop the chat event and stop the chat event functioning twice when the other round is running

ill try stop courutin also the yield i wanna try it

i know that i mean by what type of THING THING not PROBLEM THING your making

Can you read wha OP wants?

This is what OP said, and you replied with “make a debounce”


@sausagerate You can connect the .Chatted even at the beginning of the while loop and disconnect the .Chatted events at the end of the while loop via the method I showed you.

Personally I would put the .Chatted events outside of the while loop and have it read a global variable that way I don’t have to connect and disconnect a hundred times

Something like:

player.Chatted:Connect(function(message) -- obviously do this for all players
  if round == "banana" then
  end
end)

while true do 
    round = "banana"
end

also i found this article it may help you Coroutine.kill implementation - #11 by acreol and coroutine | Roblox Creator Documentation

i did the thing that u tought me but is still running and running twice also it didnt break the script but it didnt stop the event

Can you just do

if roundType ~= "Banana" then
return
end

before it runs the code
sorry for bad formatting the code might need some correction cuz I typed it in a rush
edit: fixed some errors @sausagerate

for example this

coroutine.wrap(function()
		if roundType ~= "Banana" then
			return
			elseif 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)()

Okay ima make a script and send it to you, this thread is already 40 posts long.

1 Like

If I understand what you’re trying to do alright. Which is to connect to a player.chatted event and also be able to disconnect from a player.chatted event. This should be possible without a loop. Just fire off an event with the player whose chatted event you’d like to connect to. Then you can just fire off the event.

local event = [EventReferenceHere]
event.OnServerEvent:Connect(function(player)
 connections[player] = player.Chatted:Connect(onChatted)
end)

Then you can also disconnect it.

local event = [EVENTREFERENCE]
event.OnServerEvent(function(player)
  if connections[player] then connections[player]:Disconnect() end
end)

Meanwhile, if you have the onChatted event somewhere in the script.

function onChatted(player, message, recipient)
 -- do stuff
end

To fire the event off so that it connects you’ll use event:FireServer(). No need to include any parameters because the first parameter for a remote event to the server is the player. This would be the same for disconnecting the chatted event. And of course the event names will be whatever you choose to set it as respectively for each.

1 Like

@sausagerate Try this code

code
local timer = game.ReplicatedStorage.Value.Timer

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

while true do
	for i = 1,10 do
		timer.Value = "".. 10-i
		task.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
	
	
	--connect
        local connections = {}
	for i, player in pairs(game.Players:GetPlayers()) do
		connections[player] = player.Chatted:Connect(function()
			-- code here
			-- if round == "banana" then, do all checking stuff you want here
			
			-- personally I would make this connection outside of the while loop and have it read
			-- a global value like "roundType" and then do what it wants that way I don't have to make
			-- a new connection every round and disconnect it at the end of the round
		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 = ""
	for _,player in ipairs(game.Players:GetPlayers()) do
		if players.Character ~= nil then
			connections[player]:Disconnect() -- disconnect function
			players:LoadCharacter()
			map:Destroy()
		end
	end	
end


@T0ny why are you bringing RemoteEvents into this? this is all server code.

Oh wait idk if I fully understood what you where trying to do before but now I think I get it.

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
							if roundType == "Banana" then
							print("Im an epic banana")
							end
						end
					end)
				end	
			end
		end
	end)()

try this ^

1 Like