Right, so the code I am about the present below, works perfectly fine I know it’s not the most efficient because I’m just starting off but, it works ok. Anyways, I made my script so at the start of each round, all the players that died and got removed from the table last round, got put back into the table the next time the round starts. However, this is where I run into my problem. If a person decides to kill them selves, just before the round starts, the player will still be counted into the Players alive table when the round officially starts, and they will get the coins for the round despite not even being in the round to complete it. I hope this makes sense. Here is my code:
local Players = game:GetService("Players")
local maps = game.ReplicatedStorage.Games:GetChildren()
local LobbySpawns = game.Workspace.LobbySpawn:GetChildren()
local PlayersAlive = {}
-- My Local Variables --
game.Players.PlayerAdded:Connect(function(player)-- This will fire when the player first joins the game.
local Char = player.Character or player.CharacterAdded:Wait()
local hum = Char:WaitForChild("Humanoid")
hum.Died:Connect(function() -- If the player dies, they will be removed from the alive table.
PlayersAlive[player.Name] = nil
print(player.Name, "has died!")
print(PlayersAlive)
end)
end)
game.Players.PlayerRemoving:Connect(function(player) -- if the player leaves, we want to get rid of their name in the players alive.
PlayersAlive[player.Name] = nil
print(player.Name, "has left the game. We've removed them from alive players table.")
end)
local function RoundInProgress() -- Round in progress function. Handles alive players,
for i, player in pairs(game.Players:GetChildren()) do -- Here we will loop through the players in the game and add them to the players alive table.
PlayersAlive[player.Name] = player
print(PlayersAlive)
end
for i = 10, 1, -1 do -- Starting the Round timer.
wait(1)
Message.Value = ("Round will end in: " .. i)
if i == 1 then -- When the round is over, we will reward all the players in the playersalive table (Not finished)
Message.Value = ("Round has ended!")
for i, PlayerAlive in pairs(PlayersAlive) do
local HRP = PlayerAlive.Character:FindFirstChild("HumanoidRootPart")
local RandomLobbySpawn = LobbySpawns[math.random(1, #LobbySpawns)]
PlayerAlive:FindFirstChild("leaderstats").Coins.Value += 1 -- Giving the alive player 1 more coin to their leaderstats.
HRP.CFrame = CFrame.new(RandomLobbySpawn.Position) -- Teleporting the alive player back to a random spawn in the lobby.
PlayersAlive[PlayerAlive.Name] = nil -- remove the players from the alive table. The round has finished.
print(PlayersAlive)
end
game.Workspace.CurrentGame:ClearAllChildren()
end
end
end
local function SpawnPlayers() -- Spawns the players to a random spawn on the map.
for _, SpawnPartModel in pairs(game.Workspace.CurrentGame:GetDescendants()) do
if SpawnPartModel.Name == "SpawnParts" then
local SpawnParts = SpawnPartModel:GetChildren()
for _, player in pairs(game.Players:GetChildren()) do
local Char = player.Character
local HumRootPrt = Char:FindFirstChild("HumanoidRootPart")
local randomspawn = SpawnParts[math.random(1, #SpawnParts)]
HumRootPrt.CFrame = CFrame.new(randomspawn.Position)
end
end
end
Message.Value = "Spawning Players!"
end
local function Loadmap() -- We will randomly select a map from the given location and load it into the given workspace location that we provided. "CurrentGame"
local chosenmap = maps[math.random(1, #maps)]
local CurrentMap = chosenmap:Clone()
Message.Value = ("Map Selected: " .. CurrentMap.Name)
CurrentMap.Parent = game.Workspace.CurrentGame
end
local function Intermission() -- This is just the intermission, it allows players to have a few seconds to use their coins and just chill before the next round.
for i = 6,1,-1 do
wait(1)
Message.Value = ("Intermission: " .. i)
if i == 1 then
Message.Value = ("Selecting a random map!")
end
end
end
while true do -- This will loop constantly through all of the functions that we've put into it. This will allow everything to run smoothly.
Intermission()
wait(1)
Loadmap()
wait(1)
SpawnPlayers()
wait(1)
RoundInProgress()
wait(1)
end ```