So, without getting too technical, i have a script that pretty much plays out the game. It starts the round when 2 or more players are within the game, a random map is chosen. Along with a Killer & Officer. The players spawn within the map. And the killer & officer also spawn but with selected weapons.
I'm currently confused on how i can make a constant checking system, so if a player leaves when the map is spawning or when everything is chosen and players are about to be spawned, something happens, to the point where there is only 1 player left. The current script i have just STOPS when a player leaves and there is only 1 player left. No errors in output.
I HAVE tried something, but it doesn't work and takes up too much space. Please help. Thanks. :)
Also, when answering please stick with answer my question. I don't need people commenting on how dreadful or sloppy my code is.
------------------------Variables------------------------------- minigames = game.Lighting.Minigames:GetChildren() -- Minigames Variable round = ("default") -- Round Status OfficerWep = game.Lighting.gun1 -- Officer Weapon HitmanWep = game.Lighting.gun2 -- Hitman Weapon message2 = game.Workspace.LobbyModel.Partmain.SurfaceGui.main -- The main GUI message3 = game.Workspace.LobbyModel.Partmain.SurfaceGui.timer -- The Timer GUI HitmanGUI = game.Lighting.HitmanGui OfficerGUI = game.Lighting.OfficerGui local sound = Instance.new("Sound") sound.SoundId = "http://www.roblox.com/asset/?id=130802245" sound.Parent = game.Workspace ------------------------Main------------------------------- function playMusic() sound:play() wait(10) end function checkPlayers() if game.Players.NumPlayers == 1 then wait(2) end end while true do if game.Players.NumPlayers > 1 then if round == ("default") then --Checks if a round has recently ended print("Starting default round") --This will print the first time the server is started, i think. elseif round == ("over1") then message2.Text = ("Round will start soon") wait(20) end sound:stop() checkPlayers() message2.Text = ("Randomly selecting map...") wait(3) ---------------Random select Map------------------------- checkPlayers() ranGame = math.random(1, #minigames) gameChosen = minigames[ranGame] checkPlayers() message2.Text = ("Map Chosen: " ..gameChosen.Name) wait(3) gameChosenClone = gameChosen:Clone() gameChosenClone.Parent = game.Workspace checkPlayers() message2.Text = ("Map Chosen: " ..gameChosen.Name) wait(3) message2.Text = ("Randomly selecting Hitman & Officer...") checkPlayers() wait(2) ---------------Random select hitman and officer---------- checkPlayers() Players = game.Players:GetPlayers() -- Add all players to Plrs table killer = Players[math.random(1,#Players)] -- Get a player, this player is defined under Players hero = Players[math.random(1,#Players)] ---------------Checking System---------- checkPlayers() if killer == hero then -- Checks if the selected killer/hero is the same, and if so changes it so its not repeat killer = Players[math.random(1,#Players)] wait(1) hero = Players[math.random(1,#Players)] until killer ~= hero print("The Hitman is " ..killer.Name) print("The Officer is " ..hero.Name) wait(1) end round = ("starting") ---------------Teleport players to selected map---------- spawns = gameChosenClone.Spawns:GetChildren() checkPlayers() message2.Text = ("Round in progress...") for i,v in pairs(game.Players:GetPlayers()) do name = v.Name check = game.Workspace:FindFirstChild(name) if check then checkHumanoid = check:FindFirstChild("Humanoid") if checkHumanoid then check:MoveTo(spawns[i].Position) end end end OfficerWep:Clone().Parent = hero.Backpack -- Gives the Officer Gun1 HitmanWep:Clone().Parent = killer.Backpack -- Gives the Hitman Gun2 --------------Tutorial GUI--------------------- OfficerGUI:Clone().Parent = hero.PlayerGui HitmanGUI:Clone().Parent = killer.PlayerGui wait(15) hero.PlayerGui.OfficerGui:Destroy() killer.PlayerGui.HitmanGui:Destroy() HeroDied() else playMusic() message2.Text = ("There needs to be more than 1 player to start") end wait(1) end
I believe because you have it checking if it stays up to 2 or more players, if it goes below 1, it has nothing to do. If you add it so if there is ever 1 player, the game resets and it waits until another player to start up again.