I wanted for my game to only start when it has 4 players. How do I make where once a player dies they can only spectate and once all players die, the game can restart from the beginning. Kind of the way Specter is done. I searched everywhere and could not find how to do this.
A method that you can do is adding a data folder when the game starts you can add all playing players into this folder when a player dies he will moves the “StringValue” to the spectator folder, and add a looping checker if the folder is empty so the games ends, you can use an array too local mytable = {}, but still your choice, just to dont pass to a global variable
Do you by any chance have an example script of this? I don’'t really know where to start.
Alvin blox made a great video of something like that. You can follow the tutorial and just add the spectate
My problem is finding a video or something that shows how to end the game and restart once you have all players. Does anyone maybe have a link to a youtube video or an example of a script for this?
I put the video link.______________________________________
On a server script :
while wait(1) do
local players = game.Players:GetPlayers()
if #players >= 2 then -- Only break the loop when 2 players are there (you can combine this with a check for player ready or not)
break
end
end
while wait() do -- Round loop
-- What you want the round to do
for i, player in pairs(game.Players:GetPlayers()) do
if player.Character.Humanoid.Health == 0 then
break -- This will break the round loop
end
end
Where would I add the script you gave me? In a separate ServerScriptService? Or in this one that I have below. If in this one how do I place the parts in the correct spot?
local lobbyLocation = game.Workspace.Lobby.Position + Vector3.new(0,3,0)
local gameLocation = game.Workspace.Main.Position + Vector3.new(0,3,0)
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')
local function playGame()
local timeAmount = 10
local timerText = 'Remaining Time: '
while timeAmount > 0 do
timeEvent:FireAllClients(timeAmount, timerText)
wait(1)
timeAmount -= 1
end
end
local function playIntermission()
local intermission = 5
local timerText = 'Intermission: '
while intermission > 0 do
timeEvent:FireAllClients(intermission, timerText)
wait(1)
intermission -=1
end
end
local function resetPlayers()
for _, plr in pairs(game.Players:GetChildren()) do
plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)
end
end
local function teleportPlayers()
for _, plr in pairs(game.Players:GetChildren()) do
plr.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)
end
end
while true do
resetPlayers()
playIntermission()
teleportPlayers()
playGame()
end
I also have this in
Starter Gui
Screen gui
Text label
Local script
local label = script.Parent
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')
timeEvent.OnClientEvent:Connect(function(timeAmount, timerText)
label.Text = timerText..timeAmount
end)
Took me a while to write the code from the video but I can work on adding things to the code in this video. Thanks to all that helped!!
I saw the video mentioned above 2 times and spent hours trying to find the error. The intermission, characters and map load but when all players load into the map instead of counting down the 50 seconds it will end the game after about 5 seconds and load everyone back into the intermission. The coundown from 50 to 0 is not happening. I am missing an end somewhere but I cant pinpoint it. This is the error message I get. ServerScriptService.MainScript:169: Expected ‘end’ (to close ‘do’ at line 17), got ; did you forget to close ‘then’ at line 154? Can anyone help?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 50
local reward = 25
-- Game loop
while true do
Status.Value = "Waiting for enough players"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(10)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player) --Add each player into plrs table
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--Teleport players to the map
local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
if not SpawnPoints then
print("Spawnpoints not found!")
end
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
--Teleport them
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
table.remove(AvailableSpawnPoints,1)
--Give them a sword
local ClassicSword = ServerStorage.ClassicSword:Clone()
ClassicSword.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
--There is no character
if not player then
table.remove(plrs,i)
end
end
end
end
Status.Value = "Get ready to play!"
wait(2)
for i = GameLength,0,-1 do
for x, player in pairs(plrs) do
if player then
character = player.Character
if not character then
--Left the game
table.remove(plrs,x)
else
if character:FindFirstChild("GameTag") then
--They are still alive
print(player.Name.." is still in the game!")
else
--They are dead
table.remove(plrs,x)
print(player.Name.." has been removed")
end
end
else
table.remove(plrs,x)
print(player.Name.." has been removed!")
end
end
Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
if #plrs == 1 then
--Last person standing
Status.Value = "The winner is "..plrs[1].Name
plrs[1].leaderstats.Bucks.Value = plrs[1].leaderstats.Bucks.Value + reward
break
elseif #plrs == 0 then
Status.Value = "Nobody won!"
break
else if i == 0 then
Status.Value = "Time up!"
break
end
wait(1)
end
print("End of game")
--wait(2)
for i, player in pairs(game.Players:GetPlayers()) do
character = player.Character
if not character then
--Ignore them
else
if character:FindFirstChild("GameTag") then
character.GameTag:Destroy()
end
if player.Backpack:FindFirstChild("ClassicSword") then
player.Backpack.ClassicSword:Destroy()
end
if character:FindFirstChild("ClassicSword") then
character.ClassicSword:Destroy()
end
end
player:LoadCharacter()
end
ClonedMap:Destroy()
Status.Value = "Game ended"
wait(2)
end
Found it! After this part in the code
print("End of game")
I forgot to put end. So it should be like this
print("End of game")
end