I’ve once again come into an issue and I have no clue on how to solve it. I’m making a game where I need to display the state of the game (game starting in …, intermission, boss wins, etc.)
I’m doing so using remote events and I’m unsure why this script isn’t working?
Server Script:
print statements for the round in the server run perfectly fine but I’m unsure why the client isn’t?
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local BossSelectEvent = game.ReplicatedStorage.SelectBoss
local TimeEvent = game.ReplicatedFirst:FindFirstChild("TimeEvent")
local bossString = script:FindFirstChild("Boss")
local ROUND_TIME = 60
local INTERMISSION_TIME = 15
local bossArea = Workspace:FindFirstChild("BossArea")
local playerArea = Workspace:FindFirstChild("PlayerArea")
local lobbyArea = Workspace:FindFirstChild("Lobby")
if not bossArea or not playerArea or not lobbyArea then
return
end
local function teleportCharacter(character, targetPart)
if character and targetPart then
local root = character:FindFirstChild("HumanoidRootPart")
if root then
root.CFrame = targetPart.CFrame + Vector3.new(0, 3, 0)
end
end
end
local function teleportAllPlayersToArea(targetPart)
for _, player in Players:GetPlayers() do
if player.Character then
teleportCharacter(player.Character, targetPart)
end
end
end
local function bindRespawnTeleport(targetPart, connectionTable)
-- Disconnect previous connections
for _, conn in connectionTable do
conn:Disconnect()
end
for i = 1, #connectionTable do
connectionTable[i] = nil
end
-- Bind new connections
for _, player in Players:GetPlayers() do
local conn = player.CharacterAdded:Connect(function(character)
task.wait(0.1)
teleportCharacter(character, targetPart)
end)
table.insert(connectionTable, conn)
end
end
local function getAlivePlayers(excludePlayer)
local alive = {}
for _, player in Players:GetPlayers() do
if player ~= excludePlayer and player.Character and player.Character:FindFirstChild("Humanoid") then
local humanoid = player.Character.Humanoid
if humanoid.Health > 0 then
table.insert(alive, player)
end
end
end
return alive
end
local function startRound()
local playerList = Players:GetPlayers()
if #playerList < 2 then
TimeEvent:FireAllClients("Not Enough Players!")
warn("Not enough players to start a round!")
return
end
local bossIndex = math.random(1, #playerList)
local boss = playerList[bossIndex]
bossString.Value = boss.Name
print("Boss selected:", boss.Name)
BossSelectEvent:FireClient(boss)
TimeEvent:FireAllClients("Boss selected:", boss.Name)
task.wait(5)
for _, player in Players:GetPlayers() do
if player.Character then
if player == boss then
teleportCharacter(player.Character, bossArea)
else
teleportCharacter(player.Character, playerArea)
end
end
end
local roundRespawnConnections = {}
for _, player in Players:GetPlayers() do
local conn = player.CharacterAdded:Connect(function(character)
task.wait(0.1)
if player == boss then
teleportCharacter(character, bossArea)
else
teleportCharacter(character, playerArea)
end
end)
table.insert(roundRespawnConnections, conn)
end
local alivePlayers = {}
local humanoidDiedConnections = {}
local roundEnded = false
local function endRound(bossWon)
if roundEnded then return end
roundEnded = true
for _, conn in roundRespawnConnections do
conn:Disconnect()
end
for _, conn in humanoidDiedConnections do
conn:Disconnect()
end
if bossWon then
TimeEvent:FireAllClients("Wins", boss.Name)
print("Boss wins!")
-- TODO: Reward boss
else
local survivors = 0
for player, humanoid in alivePlayers do
if humanoid.Health > 0 then
survivors = survivors + 1
end
end
print("Boss loses! Survivors:", survivors)
TimeEvent:FireAllClients("Survivors Win:", survivors)
-- TODO: Reward survivors
end
end
for _, player in Players:GetPlayers() do
if player ~= boss and player.Character and player.Character:FindFirstChild("Humanoid") then
local humanoid = player.Character.Humanoid
alivePlayers[player] = humanoid
local diedConn
diedConn = humanoid.Died:Connect(function()
alivePlayers[player] = nil
-- Teleport player to lobby on death
teleportCharacter(player.Character, lobbyArea)
-- Check if all non-boss players are dead
local stillAlive = false
for p, h in alivePlayers do
if h.Health > 0 then
stillAlive = true
break
end
end
if not stillAlive then
endRound(true) -- Boss wins
end
end)
table.insert(humanoidDiedConnections, diedConn)
end
end
-- Round timer
for i = ROUND_TIME, 1, -1 do
if roundEnded then
return
end
TimeEvent:FireAllClients("Time",ROUND_TIME)
task.wait(1)
end
-- If round not ended by deaths, check survivors
if not roundEnded then
endRound(false)
end
end
local intermissionRespawnConnections = {}
while true do
-- Intermission phase
print("Intermission! Teleporting all players to Lobby.")
teleportAllPlayersToArea(lobbyArea)
bindRespawnTeleport(lobbyArea, intermissionRespawnConnections)
for i = INTERMISSION_TIME, 1, -1 do
task.wait(1)
end
for _, conn in intermissionRespawnConnections do
conn:Disconnect()
end
for i = 1, #intermissionRespawnConnections do
intermissionRespawnConnections[i] = nil
end
-- Start round
print("Round starting!")
startRound()
-- Short break between rounds
task.wait(5)
end
Client Script:
local timeevent = game.ReplicatedFirst:FindFirstChild("TimeEvent")
timeevent.OnClientEvent:Connect(function(Var, char)
print("Recieved")
if Var == "Not Enough Players!" then
script.Parent.TimerFrame.Text.Text = "Not Enough Players!"
elseif Var == "Boss Selected:" then
script.Parent.TimerFrame.Text.Text = "Boss Selected:".. char
elseif Var == "Wins" then
script.Parent.TimerFrame.Text.Text = char.. "Wins"
elseif Var == "Survivors Win:" then
script.Parent.TimerFrame.Text.Text = "Survivors Win:".. char
end
end)

