This is code for a wait in line game, the premise is when you make it to the front in line, you fight your opponent who was the previous winner (or next in line if the game is just starting) but for some reason the function isn’t firing, im not sure what i did wrong.
this is the part of the script that i think is bugging in my main server:
local function startFightTimer()
if #PlayersInArena < MaxPlayersInArena then
FightUIEvent:FireAllClients(0, "waiting_for_opponent") --tells ui to wait
warn("not enough players to start the fight") -- debugging
return -- Doesn't start if there aren't two players
end
print("fight started! Players have 30 seconds.") -- debugging
-- fire ui event
FightUIEvent:FireAllClients(30, "fight start")
print("FightUIEvent fired for fight start") -- debugging
local fightDuration = 30
while fightDuration > 0 do
FightUIEvent:FireAllClients(fightDuration, "fight timer")
print("Timer: " .. fightDuration .. " seconds") --debugging
task.wait(1) -- Wait 1 second per loop
fightDuration -= 1
end
-- fight ended
print("Fight over!") --debugging
--check which players are still alive and determine winner
local alivePlayers = {}
for _, player in pairs(PlayersInArena) do
if player and player.Character and player.Character:FindFirstChild("Humanoid") then
if player.Character.Humanoid.Health > 0 then
table.insert(alivePlayers, player)
end
end
end
-- handle victory or draw
local winner = nil
if #alivePlayers == 1 then
winner = alivePlayers[1]
winner.Character.Humanoid.Health = winner.Character.Humanoid.MaxHealth -- restore health
print("🏆 " .. winner.Name .. " wins the fight!") -- Debugging
FightUIEvent:FireAllClients(0, "winner", winner.Name)
--give the winner a leaderboard win
winner.leaderstats.Wins.Value = winner.leaderstats.Wins.Value + 1
else
print("🥶 Draw!") -- Debugging
FightUIEvent:FireAllClients(0, "draw")
end
--send loser to the end of the line
local newPlayersInArena = {}
if #alivePlayers == 1 then
local winner = alivePlayers[1]
table.insert(newPlayersInArena, winner) -- keep the winner in the arena
end
PlayersInArena = newPlayersInArena -- only winner stays
for _, player in pairs(PlayersInArena) do
if player ~= winner then -- send the loser back in line
local lastTileNumber = #ActiveTilesFolder:GetChildren()
local lastTile = ActiveTilesFolder:FindFirstChild(tostring(lastTileNumber))
if lastTile then
playerTiles[player.UserId] = lastTile
moveToTile(player, lastTile)
print(player.Name .. " was sent to the end of the line.")
end
end
end
PlayersInArena = newPlayersInArena --reset arena
--check if there's an available player to fight the winner
for _, player in pairs(Players:GetPlayers()) do
if not table.find(PlayersInArena, player) then
if #PlayersInArena < MaxPlayersInArena then
moveToTile(player, ArenaTile) --bring in the next player
print(player.Name .. " is joining the arena.")
break
end
end
end
end
and this is the timer UI for the fight, it’s supposed to announce the fight the winner and the time in the fight left:
FightUIEvent.OnClientEvent:Connect(function(timeLeft, eventType, winnerName)
print("✅ FightUIEvent received:", eventType, " Time:", timeLeft, " Winner:", winnerName or "None") -- Debugging
if eventType == "fight_start" then
inLine.Enabled = false --disable ui
local backInLineButton = playerGui:WaitForChild("OutLine"):WaitForChild("Main"):WaitForChild("Bottom"):WaitForChild("Buttons"):WaitForChild("PLAY")
backInLineButton.Visible = false
outLine.Enabled = false
FightTimerUI.Enabled = true -- shows the fight timer
print("✅ UI Updated: Waiting for Opponent..") -- Debugging
elseif eventType == "waiting_for_opponent" then
FightTimerUI.Enabled = true
TimerLabel.Text = "Waiting for opponent.."
elseif eventType == "fight_timer" then
TimerLabel.Text = "Timer: " .. timeLeft .. " seconds"
elseif eventType == "winner" or eventType == "draw" then
TimerLabel.Visible = false
WinnerLabel.Text = winnerName .. " Wins!"
WinnerLabel.Visible = true
wait(5)
WinnerLabel.Visible = false
inLine.Enabled = true -- allow player to reenter queue
elseif eventType == "draw" then
TimerLabel.Visble = false
WinnerLabel.Text = "DRAW!"
WinnerLabel.Visible = true
wait(5)
WinnerLabel.Visible = false
inLine.Enabled = true -- allow player to reenter queue
end
end)