Table remove help.

Hello Facepunch,

I’m in need of help trying to remove the victim ( the player you press E on ) from the table when he dies. The way this is suppost to operate is, when you look at someone it’ll say ‘Not being robbed.’, and when you press E it’ll say ‘Being Robbed’.

I’ve been trying for a few hours now for a solution on how I can remove the person i press E on from the table when he dies.
Please excuse the messy code, I’m still learning.

[LUA]

–Clientside

bRobbed = {} – plrs being robed

local plr = LocalPlayer()
local isLooking = false
local isRobbing = false
bRobbed = {}

hook.Add(“Think”, “FearRP_PlrIsLooking”, function()
if not IsValid( plr ) then return end

if plr:GetEyeTraceNoCursor().Entity:IsPlayer() then
vic = plr:GetEyeTraceNoCursor().Entity
timer.Simple(0.5, function()
isLooking = true
end)

if plr:KeyDown( IN_USE ) then
plr:ChatPrint("I’m robbing "…vic:Nick())
isRobbing = true
vic.isbeingrobbed = true
end

else
timer.Simple(4, function()
isLooking = false
end)

end
end)

function startRobbery()
if not IsValid( vic ) then return end
if not vic.isbeingrobbed then

pos = vic:GetPos()
pos = pos +Vector(20,0,100)
toscreenPos = pos:ToScreen()



cam.Start2D()
	draw.RoundedBox( 4,  toscreenPos.x,  toscreenPos.y , 220, 50, Color(0,0,0,255 ) )
	draw.SimpleText("Not being robbed."..vic:Nick() ,"fearRP_FearFont2" ,toscreenPos.x + 110 ,toscreenPos.y + 20 ,Color( 255,255, 255 ), 1, 1)
cam.End2D()

else

table.insert( bRobbed, vic:Nick() )



pos = vic:GetPos()
pos = pos +Vector(20,0,100)
toscreenPos = pos:ToScreen()



cam.Start2D()
	draw.RoundedBox( 4,  toscreenPos.x,  toscreenPos.y , 220, 50, Color(0,0,0,255 ) )
	draw.SimpleText("Robbery in progress :"..bRobbed[1] ,"fearRP_FearFont2" ,toscreenPos.x + 110 ,toscreenPos.y + 20 ,Color( 255,255, 255 ), 1, 1)
cam.End2D()

end

end
hook.Add(“HUDPaint”, “FearRP_drawIconForPlayer_robbing”, startRobbery)

function fearView()

if isLooking and not isRobbing then
pos = vic:GetPos()
pos = pos +Vector(20,0,100)

toscreenPos = pos:ToScreen()



cam.Start2D()
	draw.RoundedBox( 4,  toscreenPos.x,  toscreenPos.y , 220, 50, Color(0,0,0,255 ) )
	draw.SimpleText("Press E to Rob." ,"fearRP_FearFont" ,toscreenPos.x + 110 ,toscreenPos.y + 20 ,Color( 255,255, 255 ), 1, 1)
cam.End2D()

end

end
hook.Add(“HUDPaint”, “FearRP_drawIconForPlayer”, fearView)

[/LUA]



for i=1,#bRobbed do
    if !IsValid(bRobbed*) or !bRobbed*:Alive() then
        table.remove(bRobbed,i)
    end
end


?

May I ask, what does ‘#bRobbed’ mean?

The # is the length operator. When used on tables it returns the table’s sequential length. When used on strings it returns the length of the string.

If the type is neither of the above it calls the objects __len meta method.

So as an answer, it’d return how many values are in the table right?

Yes.
I assume you’re a beginner, so, I will comment every parts of this code.



-- Earlier, you have created a table named "bRobbed"
-- In a table, you got what we'll call a "key" and a "value"
-- The key of a table can be a sequential value, numbers or strings
-- But here, your table is sequential.

-- For a better comprehension about tables, check tutorials of maurits.tv:
-- https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2584.html

-- i is a number used for the "for loop"
for i=1,#bRobbed do -- If i < #bRobbed, then repeat until i = #bRobbed, then end.
-- As I said before, a table is made of key and value, with i, check bRobbed*, if the value works.
    if !IsValid(bRobbed*) or !bRobbed*:Alive() then -- Check if bRobbed* is either invalid or dead.
        table.remove(bRobbed,i) -- If so, then, remove it from the table at key i.
    end
end