Deleting a players entities when they leave the server
4 replies, posted
So right now im setting up an inventory system, I have 80% of it done but im stuck here, when a player leaves the server, i want the server to remove all of their entities, right now I have this
[CODE]function CreateItem( ply, name, pos ) -- This function spawns the item, Works 100%
local itemT = getItems( name )
if itemT then
idd = idd + 1
local item = ents.Create( itemT.ent )
item:SetNWString( "name", itemT.name )
item:SetNWString( "itemName", name )
item:SetNWInt( "uID", idd )
item:SetNWBool( "pickup", true )
item:SetPos( pos )
item:SetNWEntity( "owner", ply ) -- THIS IS WHAT I THINK I HAVE TO TARGET TO REMOVE ALL THE ENTITIES
item:SetNWBool( "abandon", false )
item:SetSkin(itemT.skin or 0)
itemT.spawn(ply, item)
item:Spawn()
item:Activate()
else
return flase
end
end
function ply:databaseDisconnect( ply ) --This is called when a player leaves, works
if self:GetNWInt("itemsout") > 0 then
-- RIGHT HERE IS WHERE THE FUNCTION TO REMOVE ALL THE PLAYERS ENTITIES WOULD BE--
self:databaseSetValue("itemsout", 0)
end
self:computeWeight()
self:databaseSave()
print("distest")
end
[/CODE]
I think I would have to use a while loop or for loop but I dont know how i would do it, heres how I think about it without code
gather all the entities, if the entity owner is ply, then, ent:remove
[lua]for k, v in ipairs(ents.FindByClass("your entity class here")) do
if (v:GetNWEntity("owner") == ply) then
v:Remove()
end
end[/lua]
But I think it's better you just keep track of every item of theirs in a table and loop through that. Much faster and easier to do it that way. Just a simple table attached to the player, and you table.insert the item in the CreateItem function.
[QUOTE=sannys;51204980][lua]for k, v in ipairs(ents.FindByClass("your entity class here")) do
if (v:GetNWEntity("owner") == ply) then
v:Remove()
end
end[/lua]
But I think it's better you just keep track of every item of theirs in a table and loop through that. Much faster and easier to do it that way.[/QUOTE]
If I were to put them in a table, how would I loop through that?
[QUOTE=Yikegaming;51204994]If I were to put them in a table, how would I loop through that?[/QUOTE]
[lua]
ply.items = ply.items or {}
-- Create the item and such
table.insert(ply.items, item)
-- Then, when they disconnect
for k, v in ipairs(ply.items) do
if IsValid(v) then
v:Remove()
end
end[/lua]
You should use a more unique name for the items table. That's just an example. You should also remove the item from the table when it's removed by other means.
[QUOTE=sannys;51205012][lua]
ply.items = ply.items or {}
-- Create the item and such
table.insert(ply.items, item)
-- Then, when they disconnect
for k, v in ipairs(ply.items) do
if IsValid(v) then
v:Remove()
end
end[/lua]
You should use a more unique name for the items table. That's just an example. You should also remove the item from the table when it's removed by other means.[/QUOTE]
Thanks for your help, I definetly know im getting somewhere lol but now I get this error
[CODE]attempt to index a string value with bad key ('IsValid' is not part of the string library)[/CODE]
[editline]15th October 2016[/editline]
[QUOTE=Yikegaming;51205336]Thanks for your help, I definetly know im getting somewhere lol but now I get this error
[CODE]attempt to index a string value with bad key ('IsValid' is not part of the string library)[/CODE][/QUOTE]
So I solved it, my issue was, I was storing the name of the entity and not the entity itself, its all done, thanks!
Sorry, you need to Log In to post a reply to this thread.