Greetings,
There is a similar topic to this, but I’m a little too rookie to understand the explanations offered and how I’d need to go about changing this code.
I currently have a .Died event linked to respawning a character into an arena. Previously I used .CharacterAppearanceLoaded, however this would end up causing issues as the wait time for respawn is too long for smooth functionality and it would respawn the wrong people on many occasions.
I was using .Died to begin with, but the main issue is that after the first death, the event would disconnect due to how the code runs, the new character was not detected for the event. I’ve tried reconnecting it, but it was not as easy as :Connect().
I am using built-in arrays and dictionaries to store many values such as the players, their lives and their teams.
universePlayers is a table storing the Players.Player instance
universeLives and UniverseTeams store the .Name of universePlayers alongside their indices.
Enough talking, here’s the code:
local deathevent
for i = 1, #universePlayers do
deathevent = workspace:FindFirstChild(universePlayers[i].Name).Humanoid.Died:Connect(function()
if not universePlayers[i] then
return warn("Tracking remove.")
end
universeLives[universePlayers[i].Name] -= 1
print(universePlayers[i].Name .. " has " .. universeLives[universePlayers[i].Name] .. " lives left.")
if universeLives[universePlayers[i].Name] == 0 then
--spectate(universeLives[i])
print(universePlayers[i].Name .. " is out.")
deathevent:Disconnect()
local isTeamOut = 0
if universeTeams[universePlayers[i].Name] == 1 then
for i,v in pairs(universeTeams) do
if v == 1 then
if universeLives[i] == 0 then
isTeamOut += 1
end
end
end
elseif universeTeams[universePlayers[i].Name] == 2 then
for i,v in pairs(universeTeams) do
if v == 1 then
if universeLives[i] == 0 then
isTeamOut += 1
end
end
end
end
print("Team out check =" .. isTeamOut)
if isTeamOut == 2 then
if universeTeams[universePlayers[i].Name] == 1 then
for i,v in pairs(universeTeams) do
if v == 2 then
local duelPlayerCharacter = workspace:FindFirstChild(i)
local duelPlayer = Players:GetPlayerFromCharacter(duelPlayerCharacter)
duelPlayer.leaderstats.Wins.Value = duelPlayer.leaderstats.Wins.Value + 1
print(duelPlayer.Name .. " wins!")
deathevent:Disconnect()
end
end
else
for i,v in pairs(universeTeams) do
if v == 1 then
local duelPlayerCharacter = workspace:FindFirstChild(i)
local duelPlayer = Players:GetPlayerFromCharacter(duelPlayerCharacter)
duelPlayer.leaderstats.Wins.Value = duelPlayer.leaderstats.Wins.Value + 1
print(duelPlayer.Name .. " wins!")
deathevent:Disconnect()
end
end
end
else
return
end
gameEnd = true
else
print("Respawning")
if #universePlayers == 2 then
for player = 1, #universePlayers do
print(universePlayers[player])
respawnInformation(universePlayers[player])
end
fullRespawnShield()
else
print(universePlayers[i])
warn(universePlayers[i].Name .. "respawn sequence started. This should not respawn other players.")
universePlayers[i]:LoadCharacter()
universePlayers[i].CharacterAppearanceLoaded:Wait()
respawnInformation(universePlayers[i])
deathevent:Connect()
singleRespawnShield(universePlayers[i])
end
end
end)
end
The top portion deals with taking the player out of the game if he has 0 lives, and checking if his teammates also do, in which case, the game ends.
The bottom portion deals with respawn, where I’d need the .Died event reapplied.
edit:
SOLUTION CODE BELOW
local deathevent
local function createDeathEvent(player)
--if not table.find(universePlayers, player) then
-- deathevent:Disconnect()
-- return warn("Tracking remove.")
--end
local humanoid = workspace:FindFirstChild(player.Name).Humanoid
deathevent = humanoid.Died:Connect(function()
universeLives[player.Name] -= 1
print(player.Name .. " has " .. universeLives[player.Name] .. " lives left.")
if universeLives[player.Name] == 0 then
--spectate(universeLives[i])
print(player.Name .. " is out.")
deathevent:Disconnect()
local isTeamOut = 0
if universeTeams[player.Name] == 1 then
for i,v in pairs(universeTeams) do
if v == 1 then
if universeLives[i] == 0 then
isTeamOut += 1
end
end
end
elseif universeTeams[player.Name] == 2 then
for i,v in pairs(universeTeams) do
if v == 1 then
if universeLives[i] == 0 then
isTeamOut += 1
end
end
end
end
print("Team out check =" .. isTeamOut)
if isTeamOut == 2 then
if universeTeams[player.Name] == 1 then
for i,v in pairs(universeTeams) do
if v == 2 then
local duelPlayerCharacter = workspace:FindFirstChild(i)
local duelPlayer = Players:GetPlayerFromCharacter(duelPlayerCharacter)
duelPlayer.leaderstats.Wins.Value = duelPlayer.leaderstats.Wins.Value + 1
print(duelPlayer.Name .. " wins!")
deathevent:Disconnect()
end
end
else
for i,v in pairs(universeTeams) do
if v == 1 then
local duelPlayerCharacter = workspace:FindFirstChild(i)
local duelPlayer = Players:GetPlayerFromCharacter(duelPlayerCharacter)
duelPlayer.leaderstats.Wins.Value = duelPlayer.leaderstats.Wins.Value + 1
print(duelPlayer.Name .. " wins!")
deathevent:Disconnect()
end
end
end
else
return
end
gameEnd = true
else
print("Respawning")
if #universePlayers == 2 then
for player = 1, #universePlayers do
print(universePlayers[player])
respawnInformation(universePlayers[player])
end
fullRespawnShield()
else
warn(player.Name .. "respawn sequence started. This should not respawn other players.")
player:LoadCharacter()
local character = player.CharacterAppearanceLoaded:Wait()
local newPlayer = Players:GetPlayerFromCharacter(character)
respawnInformation(player)
createDeathEvent(newPlayer)
singleRespawnShield(player)
end
end
end)
end
for i = 1, #universePlayers do
createDeathEvent(universePlayers[i])
end