• Having a problem with checking if an entity is in the table and adding entities.
    7 replies, posted
Hey, so the title is pretty self explanatory; I'm having trouble adding entities to a table and checking if that specific entity is in it. So for adding them; I used table.insert and PrintTable to check if it were added; it worked and added it but it adds a bunch of the same entity: 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] 1    =    Player [2][Bot07] And for removing them I looped through the table and checked if the entity was not alive or not valid: for k, targetPly in pairs(PlayersSentToPD) do     if not IsValid(targetPly) or not targetPly:Alive() then      table.RemoveByValue(PlayersSentToPD, targetPly) end end And to not add the player to the table again I did this: if targetPly:GetPos():Distance(self.Owner:GetPos()) <= 50 and not table.HasValue(PlayersSentToPD, targetPly) then Full code is here: function SWEP:PrimaryAttack()     targetPly = self.Owner:GetEyeTrace().Entity     local PlayersSentToPD = {}     if not IsValid(targetPly) then return end     if targetPly:GetPos():Distance(self.Owner:GetPos()) <= 50 and not table.HasValue(PlayersSentToPD, targetPly) then         targetPly:SetPos(PDLocation)         table.insert(PlayersSentToPD, targetPly)         PrintTable(PlayersSentToPD)         timer.Simple(5, function()             if SERVER then self.Owner:Freeze() self.Owner:EmitSound("scp106/corrosion2.ogg", math.random(50,100), math.random(90,110), 0.9) end                          timer.Create("PlayerGoingToPD", 0.1, 13, function()                 self.Owner:SetPos(self.Owner:GetPos() + Vector(0, 0, -5))             end)                          timer.Simple(1.5, function()                 self.Owner:SetPos(PDLocation)             end)             timer.Simple(15, function()                 self.HasPlayerReturnedToPD = false             end)         end)         for k, targetPly in pairs(PlayersSentToPD) do             if not IsValid(targetPly) or not targetPly:Alive() then                 table.RemoveByValue(PlayersSentToPD, targetPly)             end         end     end end
take a look into your printable, all of it only has a single index, 1 That might mean your problem is prediction, try to check if you are running only in IsFirstTimePredicted, if your table would have too many times the same ent, it would output different indexes
I did if not IsFirstTimePredicted() then return end It seems to lower the count but it still adds 3 of the same entity to the table now.
It's not the same entity multiple times, it's the same table with the only entity printed multiple times, this is how does your table looks now 1 = Player [2][Bot07] 1 = Player [2][Bot07] 1 = Player [2][Bot07] That means table PlayersSentToPD will only contains 1 item, bot07, in case it contains multple times the same entity, it would look like 1 = Player [2][Bot07] 2 = Player [2][Bot07] 3 = Player [2][Bot07] Since tables are indexed by unique values, so you're safe that your PrimaryFire runs multiple times in Clientside, since prediction, meanwhile in server there's no such thing because you don't need to interpolate, so it runs only once
I'm a bit confused by your primary fire function. What exactly are you trying to achieve? Not only are you creating a new table every time you fire, but you are immediately removing them from that table after you add them. You are also setting player position down -5 before sending them out. That's a bit odd. And your player sent to PD variable is getting set false 15 seconds after they were already set? Couldn't you basically achieve the same thing with this? function SWEP:PrimaryAttack() if !SERVER then return end local trace = self.Owner:GetEyeTrace() local ent = trace.Entity if !IsValid(ent) then return end if !ent:IsPlayer() then return end if ent:GetPos():Distance(self.Owner:GetPos()) > 50 then return end ent:SetPos(PDLocation) timer.Simple(5, function() self:EmitSound("scp106/corrosion2.ogg", math.random(50,100), math.random(90,110), 0.9) self.Owner:Freeze() self.Owner:SetPos(PDLocation) end) end Entity/EmitSound Note: "When using this function with weapons, use the Weapon itself as the entity, not its owner!"
The whole SetPos -5 thing is to give the effect of SCP-106 going through the floor to get to the Pocket Dimension.
Now, how do I check if the entity exists in a table?
Both negative and positive values are supported when you use sethealth. Either use Entity/TakeDamage or check if their health goes below 0 then kill them.
Sorry, you need to Log In to post a reply to this thread.