I have looked at other peoples posts but none of them make sense to me. I would like to stop a round when all people die, but my solution has some glitches
while true do
game.ReplicatedStorage.PeopleAlive.Value = game.ReplicatedStorage.PlayersInTheServer.Value - game.ReplicatedStorage.PlayersInGame.Value
wait()
end
This script detects what players are left.
db = false
while true do
if game.ReplicatedStorage.Status.Value == 2 then
game.ReplicatedStorage.TPBlock:Clone().Parent = game.Workspace
wait(3)
local rand = Random.new()
local music = game.Workspace.Music.MapMusic:GetChildren()
local chosenmusic = music[rand:NextInteger(1,#music)]
chosenmusic:Play()
game.ReplicatedStorage.TimeLeft.Value = game.ReplicatedStorage.Time.Value
repeat
game.ReplicatedStorage.TimeLeft.Value = game.ReplicatedStorage.TimeLeft.Value-1
wait(1)
until game.ReplicatedStorage.TimeLeft.Value == 0 or game.ReplicatedStorage.PeopleAlive.Value == 0
if game.ReplicatedStorage.Status.Value == 2 then
game.ReplicatedStorage.PlayersInGame.Value = 0
game.ReplicatedStorage.ItsOver:Clone().Parent = game.Workspace
game.ReplicatedStorage.Status.Value = 0
game.ReplicatedStorage.DeleteMap.Value = 1
for i = 1, 10 do
chosenmusic.Volume = chosenmusic.Volume-0.1
wait(0.1)
end
chosenmusic:Stop()
chosenmusic.Volume = 1
chosenmusic.TimePosition = 0
elseif game.ReplicatedStorage.PeopleAlive.Value == 0 then
game.ReplicatedStorage.Status.Value = 0
game.ReplicatedStorage.DeleteMap.Value = 1
for i = 1, 10 do
chosenmusic.Volume = chosenmusic.Volume-0.1
wait(0.1)
end
chosenmusic:Stop()
chosenmusic.Volume = 1
chosenmusic.TimePosition = 0
end
This script includes my music, my time, and the players left.
My problem is, if someone leaves after dying, it will still keep a point. However, if they didn’t die, it wouldn’t do that .
I can easily fix that problem, but if I do, it will still glitch out if they leave and don’t die.
Thank you for reading and I hope I can get an answer.
Well you could just make a loop and based on that when the PlayersInGame value is 0 or nil then you can kill every player.
local RunService = game:GetService("RunService")
RurnService.Hearbeat:Connect(function()
if game.ReplicatedStorage.PlayersInGame.Value == 0 or nil then
for i,Player in pairs(game.Players:GetChildren()) do
Player.Character:WaitForChild("Humanoid"):TakeDamage(100) -- Default Player Health
end
end
end)
I have already got one though. My problem is the people leaving without dying, even if I did make the death value rise by 1, if they already died then it would be an unnecessary point on the dead players value.
The way I would do it is store all the players in the round in a table and add/remove them when needed. When you remove someone check if the table is empty and end the round.
Here’s a very simple round system that I like to use as a template.
-- Create global variables, and remove players from round if they leave
local playersInRound = {}
local MINIMUM_PLAYERS = 2 -- minimum players to start round
local INTERMISSION_LENGTH = 5
local ROUND_LENGTH = 30
local function removePlayerFromRound(player)
local index = table.find(playersInRound, player)
if index then
print(player, "has left the round")
table.remove(playersInRound, index)
end
end
game.Players.PlayerRemoving:Connect(removePlayerFromRound)
while true do
--// Intermission
for i = INTERMISSION_LENGTH, 1, -1 do
print("Intermission..", i)
wait(1)
end
-- choose map/music here
--// Add players to round list
playersInRound = {} -- Reset players table. Store all players that will be in the round
local connections = {} -- store connections used to disconnect later
-- add all players to table
for i, player in pairs(game.Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("Humanoid") then -- make sure player is loaded in before adding
-- teleport player or something here
table.insert(playersInRound, player)
connections[player.Name] = player.Character.Humanoid.Died:Connect(function() -- remove player from table on death
removePlayerFromRound(player)
end)
end
end
if #playersInRound < MINIMUM_PLAYERS then
print("Not enough players to begin round!")
continue -- repeat to top of loop
end
--// Start round
local roundLength = ROUND_LENGTH
while roundLength > 0 and #playersInRound > 0 do -- play round until either time is up or all players died
print("Playing round", roundLength)
roundLength = roundLength - 1
wait(1)
end
--// Round ended
if roundLength > 0 then
print("All players have died!") -- round ended by all players dying
else
print("Time's up!") -- round ended because time is up
end
--// Disconnect connections to prevent memory leaks
for i, connection in pairs(connections) do
connection:Disconnect()
end
-- unload map and stop music here
-- Script will now loop back to top and repeat
end
Don’t give up! If you give up, then you may never continue scripting again! This is how you can make a table to put all the players into:
local PlayerTable = {}
game.Players.PlayerAdded:Connect(function(plr)
table.insert(PlayerTable, plr)
end)
What I did in the above script is pretty simple. I just made a table then wrote that when a player is added into the game, we will put them in the table. However, this will just make a table for when the player joins, not when the round starts. You can make a new table for when then round starts by looping through the players and inserting them into the table by writing the below code:
local playersInRound = {}
for i, player in pairs(game.Players:GetPlayers()) do
table.insert(playersInRound, player)
end
You can then detect how many players are in the round. However, I haven’t tested this in studio, nor have I put a script to take out players from the table. That part is up to you.
I personally haven’t been scripting for very long, but if there is one thing I learned, it’s to never give up.
P.S: You can probably find a better answer than what I gave here. I can’t access studio right now so I can’t be sure.
I really have no more ideas and I really just want to quit now because I will be at this script for months just for the game to get 100 views and then be forgotten about.
You can use them as a base to work on if you don’t know where to start and build upon them. Add or remove parts you want or maybe just read the code to understand it and make your own system.