• Doubt with some tables
    4 replies, posted
Hi! I'm trying to make a script that adds a halo on the traitors who are alive if the locaplayer is traitor. The problem comes when trying to remove the traitors who die from the traitors table. If i print the table the output looks like this : [CODE] traidores : = { Player [2][Bot41], Player [4][Bot42], Player [6][Bot43], Player [8][Bot44], } [/CODE] When for example the bot42 dies, bot42 is not removed from the table, but the fourth player on the table is removed instead. In other words, when a player dies, the player who is in the table position of the brackets will be removed instead of himself. ( player[4] dying would remove the fourth player of the table) This is the code that removes the players who die, any ideas of what is wrong? (the traitors table is shared) [CODE] if SERVER then util.AddNetworkString( "borrart" ) end net.Receive("borrart",function(len,ply) table.RemoveByValue(traidores, net.ReadEntity()) print( table.ToString(traidores,"traidores : " , true)) -- <- Just to see the table after removing the player who died end) hook.Add("PlayerDeath", "borrartraidores", function(victim, inflictor, attacker) for k,v in pairs(traidores) do if victim == v then net.Start("borrart") net.WriteEntity(v) net.Send(traidores) table.RemoveByValue(traidores, v) end end end) [/CODE]
why not make it like this: [CODE]hook.Add("PreDrawHalos", "traitors", function() if LocalPlayer():GetRole() != ROLE_TRAITOR then return end local traitors = {} for _, ply in ipairs(player.GetAll()) do if ply:GetRole() == ROLE_TRAITOR and ply:Alive() then table.insert(traitors, ply) end end halo.Add(traitors, Color(255, 0, 0)) end)[/CODE]
A for loop each frame? I thought about it but its not good to make a for loop each frame to the players, isnt it?
what about this? [CODE]local traitors = {} hook.Add("TTTBeginRound", "gettraitors", function() traitors = {} if LocalPlayer():GetRole() == ROLE_TRAITOR then for _, ply in ipairs(player.GetAll()) do table.insert(traitors, ply) end end end) hook.Add("PreDrawHalos", "traitors", function() if LocalPlayer():GetRole() != ROLE_TRAITOR then return end for k, ply in ipairs(traitors) do if !IsValid(ply) or !ply:Alive() then table.remove(traitors, k) break end end halo.Add(traitors, Color(255, 0, 0)) end)[/CODE]
Well I thought making a for loop each frame to the players would lag them but it seems it doesn't, thank you [:
Sorry, you need to Log In to post a reply to this thread.