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
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
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)()
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.
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)()
local playersService = game:GetService("Players")
local repstorage = game:GetService("ReplicatedStorage")
local timer = repstorage:WaitForChild("Value"):WaitForChild("Timer")
local maps = repstorage:WaitForChild("Maps"):GetChildren()
while task.wait() do
for i = 10,0,-1 do
timer.Value = "".. i
task.wait(1)
end
local rand = math.random(1,#maps)
local map = maps[rand]:Clone()
map.Parent = workspace:WaitForChild("Map")
local players = playersService:GetPlayers()
for i = 1,#players do
if players[i].Character ~= nil then
local spawns = map:WaitForChild("Spawn"):GetChildren()
local teleporter = math.random(1,#spawns)
players[i].Character:MoveTo(spawns[teleporter].Position)
players[i].Character.Parent = workspace:WaitForChild("Ingame")
end
end
local roundLength = 50
local roundType = ""
if map:FindFirstChild("Banana") then
roundType = "Banana"
roundLength += 30
elseif map:FindFirstChild("Mango") then
roundType = "Mango"
roundLength -= 10
end
coroutine.wrap(function()
if roundType == "Banana" then
local players = playersService:GetPlayers()
for i = 1,#players do
if players[i].Character ~= nil then
players[i].Chatted:Connect(function(msg)
if string.lower(msg) == "banana" then
print("Im an epic banana")
end
end)
players[i].Chatted:Disconnect()
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 string.lower(msg) == "mango" then
print("Im an epic mango")
end
end)
players[i].Chatted:Disconnect()
end
end
end
end)
repeat
roundLength -= 1
timer.Value = ""..roundLength
task.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
task.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