Hello, the code below is code for a basic round system but I’ve run into a bug that I can’t seem to fix. Basically, when the chosen player leaves the game mid-match everyone is supposed to be teleported back to the lobby and the intermission will continue as usual. The bug is that after everyone is teleported back to the lobby, the intermission text gui just says “Intermission: 0” for the duration of the game time (after this the game resumes to normal). So if you know how to fix this, please let me know! Also if you need the code for the text gui’s let me know.
local rp = game:GetService("ReplicatedStorage")
local countdownvalue = rp:WaitForChild("CountdownValue")
local ingamevalue = rp:WaitForChild("InGameValue")
local ingamecountdownvalue = rp:WaitForChild("InGameCountdownValue")
local minplayers = 2
local teleportchosenpart = game.Workspace:WaitForChild("TeleportChosen")
local teleportpart = game.Workspace:WaitForChild("TeleportPart")
local lobbyspawn = game.Workspace:WaitForChild("LobbySpawn")
function mainfunction()
print("at the start")
for i = 10,0,-1 do
countdownvalue.Value = i
wait(1)
if #game:GetService("Players"):GetPlayers() < minplayers then
return
end
end
for _,plr in ipairs(game.Players:GetChildren()) do
plr.Character.HumanoidRootPart.CFrame = teleportpart.CFrame
end
local players = game.Players:GetPlayers()
local chosenplayer = players[math.random(#players)]
local chosenplayercharacter = chosenplayer.Character or chosenplayer.CharacterLoaded:Wait()
chosenplayercharacter.HumanoidRootPart.CFrame = teleportchosenpart.CFrame
ingamevalue.Value = true
for i = 15,0,-1 do
ingamecountdownvalue.Value = i
wait(1)
game.Players.PlayerRemoving:Connect(function(player)
print("in removed")
if player.Name == chosenplayer.Name then
print("in main")
for _,plr in ipairs(game.Players:GetChildren()) do
plr.Character.HumanoidRootPart.CFrame = lobbyspawn.CFrame
end
ingamevalue.Value = false
return
end
end)
end
ingamevalue.Value = false
for _,plr in ipairs(game.Players:GetChildren()) do
plr.Character.HumanoidRootPart.CFrame = lobbyspawn.CFrame
end
end
while true do
wait()
if #game:GetService("Players"):GetPlayers() >= minplayers then
mainfunction()
else
countdownvalue.Value = "Waiting for players..."
end
end
So now you have explained the problem, what is your goal here? I don’t understand what you are trying to do.
Please reread your post which you have made NOT LONG AGO before you even post that reply. That is complete misinformation which has nothing to do with the OP’s goal.
Hi, the goal is to remove the bug of it saying “Intermission: 0” for the duration of the game time, instead it should instantly start the intermission without waiting for the duration of the game time to end.
In that case, you need to make a variable where it detects when a specific player leaves the game. From your code it looks like you are connecting the Players.PlayerAdded event everytime the for loop completes one iteration which is unnecessary memory leak and can tank up the server’s performance.
You should only connect the event once, and when a player leaves the game, set a variable to true boolean value, then whenever the for loop is doing one iteration, check if that variable is true, and if it is, that means you need to break the for loop.
In my code I’d only want the script to break if the chosen player leaves, but with the explanation above how would I detect if it is the chosen player who left?
No. The return statement is used when you use a function and the function is expected to give back a value. The break statement is used when you want to stop a for or while loop from continuing its iterations.
Dont read the wrong thing sent by people,go and read the things,there is also one post about cannot teleport to that location due to the mixups of getplayers and get children
Dont read long time ago posts,they are not so accurate anymore
It is still the main cause of why the intermission is stuck at zero as a print output,cause it cant identify the character again.
No. Thé return statement in your current code does not restart the whole thing. This return statement is used to stop the anonymous function inside the event.
game.Players:GetPlayers() will look for CHILDREN that are players. gameplayers:GetChildren() will look for CHILDREN that can be anything, not just players.
Hi your script is currently creating a new PlayerRemoving connection each time you loop over it, which is 15 times for each round. Keep any connection outside of the round script (if possible) or at least disconnect the connection at the end of the loop to prevent duplicates.
Currently after 5 rounds you’re having 75 duplicate connections of the same event.
Good practice is to create connections outside any loops if possible, since there is no need to disconnect or create connections on new, that you use very frequently. Create it once, use it often, disconnect when not used in the near future.
Also use GetPlayers over GetChildren when fetching players, do not listen to hellothere.