• Making a certain entity remove when you change job? (DarkRP)
    32 replies, posted
Ok basically imagine you bought a Money Printer (Which is an entity) and the only job that could have it is TEAM_PRINTER and if you change job then how would you make it remove the entity! I have been looking for the code that removes shipments when you change job but I could not find it? Any help would be much appreciated Thanks in advance :)
Well, it depends on what version of DarkRP you are using. DarkRP versions that are less than 2.5 didn't have a hook for OnPlayerChangedTeam. (At least from my experience with it, it didn't work) If you had 2.5 or greater. I'd do something like this: [code] hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_PRINTER then for k, v in pairs(ents.FindByClass("money_printer")) do if v.SID == ply.SID then v:Remove() end end end end) [/code] Not quite sure about the v.SID == ply.SID part, I derived this from the Wyozi media center addon from Coderhire, because unfortunately it was designed for DarkRP 2.5, and I had 2.4.3, I had to make my own hook.call inside the server/player.lua file that sent their job, etc.
[code] hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_PRINTER then for k, v in pairs(ents.FindByClass("money_printer")) do if v:Getowning_ent() == ply then v:Remove() end end end end) [/code]
Ok perfect :) But how would I allow multiple entity's???
[QUOTE=MoltoCraft;45367061]Ok perfect :) But how would I allow multiple entity's???[/QUOTE] What? That deletes all the players money printers if they change from the TEAM_PRINTER team.
The code doesn't work anymore? I now get this error?!? [url]http://i.imgur.com/jzQji4v.png[/url] It litrelly worked a day ago and now it broke? Does anyone know the problem? p.s. Does anyone also now how to get multiple entity's on the list instead of just 1
I am 100% sure that there is a setting in DarkRP that removes entities if the person who bought them changes teams... I can remember me getting angry because shipments dissapeared once the gundealer changes jobs... [editline]13th July 2014[/editline] [code]-- removeclassitems - Enable/disable shipments/microwaves/etc. removal when someone changes team. GM.Config.removeclassitems = true[/code] Told you...
No there isn't, but that code I was given worked before and now I get this error ([url]http://i.imgur.com/jzQji4v.png[/url]) and I was wondering if an update has changed the code or something? p.s. GM.Config.removeclassitems = true (This code only removes shipments but not a certain entity)
[QUOTE=MoltoCraft;45379379]No there isn't, but that code I was given worked before and now I get this error ([url]http://i.imgur.com/jzQji4v.png[/url]) and I was wondering if an update has changed the code or something? p.s. GM.Config.removeclassitems = true (This code only removes shipments but not a certain entity)[/QUOTE] Did you even read the config I just gave you?
Yes, and you don't seem to listen... [editline]13th July 2014[/editline] Does anyone know why the code suddenly broke after 1 day? This is a link to the error I recieve "http://i.imgur.com/jzQji4v.png"
[QUOTE=MoltoCraft;45379644]Yes, and you don't seem to listen... [editline]13th July 2014[/editline] Does anyone know why the code suddenly broke after 1 day? This is a link to the error I recieve "http://i.imgur.com/jzQji4v.png"[/QUOTE] It does if you set up your entities properly. It says "Class entities", meaning, entities belonging to a specific class (Job).
What are you talking about there is no way to set this up to make certain entity's dissepear!!! Now can anyone help me figure out whats wrong which the code!
Good luck with your help.
[QUOTE=MoltoCraft;45380049]What are you talking about there is no way to set this up to make certain entity's dissepear!!! Now can anyone help me figure out whats wrong which the code![/QUOTE] If you actually read the error you got... It's trying to get the owner of an entity (the type you put in ents.FindByClass("THIS")), but the entity hasn't setup data tables to contain the owning_ent.
Hey Ill have something for you in a sec just gotta boot up my laptop [Code] local entsToRemove = { "whateverthefuckyouwant", "sameasabove", } local jobsincluded = { TEAM_WHATEVER, TEAM_WHATEVER2, } local function CheckKillEnts( ply, oldTeam, Newteam ) if oldTeam == jobsincluded[ply:Team()] then for _, entClass in ipairs( entsToRemove ) do for id, ent in pairs( ents.FindByClass( entClass ) ) do if ent.dt.owning_ent == ply then ent:Remove() end end end end end hook.Add( "OnPlayerChangedTeam", "Destroythemoldents", CheckKillEnts ) [/Code] goes into lua/autorun/server
It doesn't work?, there is also no errors but I don't see any errors! [editline]14th July 2014[/editline] Anyone got any ideas?
[QUOTE=Marijuanaman;45380901]Hey Ill have something for you in a sec just gotta boot up my laptop [Code] local entsToRemove = { "whateverthefuckyouwant", "sameasabove", } local jobsincluded = { TEAM_WHATEVER, TEAM_WHATEVER2, } local function CheckKillEnts( ply, oldTeam, Newteam ) if oldTeam == jobsincluded[ply:Team()] then for _, entClass in ipairs( entsToRemove ) do for id, ent in pairs( ents.FindByClass( entClass ) ) do if ent.dt.owning_ent == ply then ent:Remove() end end end end end hook.Add( "OnPlayerChangedTeam", "Destroythemoldents", CheckKillEnts ) [/Code] goes into lua/autorun/server[/QUOTE] You haven't made the team number index, meaning that when you check if the old team is in the table, you actually try to check if the auto-assigned index is the team number. Untested: [lua] local entsToRemove = { "whateverthefuckyouwant", "sameasabove", } local jobsincluded = { [TEAM_WHATEVER] = true, [TEAM_WHATEVER2] = true, } hook.Add( "OnPlayerChangedTeam", "Destroythemoldents", function(ply, old, new) if jobsincluded[old] then for k, entClass in ipairs(entsToRemove) do for id, ent in pairs(ents.FindByClass(entClass)) do if ent.dt.owning_ent == ply then ent:Remove() end end end end end) [/lua]
That didn't work either, but it's strange because I get no error's Anyone know the issue?
Might be because the TEAM_* vars are undefined when the script loads.
So how do I define them?
[QUOTE=MoltoCraft;45386266]So how do I define them?[/QUOTE] Well you could check by job names instead? team.GetName(ply:Team()) == "My Job"
It didn't work? All I simply want to do is make an entity dissepear when you change job? [editline]14th July 2014[/editline] [CODE]hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_PRINTER then for k, v in pairs(ents.FindByClass("money_printer")) do if v.SID == ply.SID then v:Remove() end end end)[/CODE] That code was working but I now get this error: [url]http://i.imgur.com/jzQji4v.png[/url] Anyone know why it's doing that, and if you do then can you fix it?
[QUOTE=MoltoCraft;45387212]It didn't work? All I simply want to do is make an entity dissepear when you change job? [editline]14th July 2014[/editline] [CODE]hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_PRINTER then for k, v in pairs(ents.FindByClass("money_printer")) do if v.SID == ply.SID then v:Remove() end end end)[/CODE] That code was working but I now get this error: [url]http://i.imgur.com/jzQji4v.png[/url] Anyone know why it's doing that, and if you do then can you fix it?[/QUOTE] Go to the shared of the money printer Make sure you have this [code] function ENT:SetupDataTables() self:NetworkVar("Entity", 0, "owning_ent") end [/code]
Just do this. This works perfectly fine. Tested it myself, you don't need to call some dumb method when you can get straight to the value yourself. [code] hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_PRINTER then for k, v in pairs(ents.FindByClass("money_printer")) do if Isvalid(v.dt.owning_ent) and IsValid(ply) and v.dt.owning_ent == ply then v:Remove() end end end end) [/code] Although not traditionally done with v.dt.owning_ent, it still gets the job.
[QUOTE=insanekid2;45389550]Just do this. This works perfectly fine. Tested it myself, you don't need to call some dumb method when you can get straight to the value yourself. [code] hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_PRINTER then for k, v in pairs(ents.FindByClass("money_printer")) do if v.dt.owning_ent == ply then v:Remove() end end end) [/code] Although not traditionally done with v.dt.owning_ent, it still gets the job.[/QUOTE] You can't possibly have tested that and still say it works. You missed an end to your if statement.
[QUOTE=ms333;45389647]You can't possibly have tested that and still say it works. You missed an end to your if statement.[/QUOTE] My bad, copied it from a larger function, and slapped on an end). We all mistakes don't we? Edited and fixed it. Came from this originally. [code] hook.Add("InsaneChangeTeam", "FixedTheWyoziRemoveEntityBecauseOfBadHook", function(ply, prevTeam, t) if prevTeam == TEAM_RADIODJ then wyozimc.Debug("RadioDJ ", ply, " changed job, finding old radios owned by him ", ply.SID) for k, v in pairs(ents.FindByClass("wyozi_bc_masterradio")) do if v.SID == ply.SID then wyozimc.Debug(ply, " changed job. Removing his old radio ", v) v:Remove() ply["maxwyozi_bc_masterradio"] = (ply["maxwyozi_bc_masterradio"] or 1) - 1 end end for k, v in pairs(ents.FindByClass("wyozi_discoball")) do if v.SID == ply.SID then wyozimc.Debug(ply, " changed job. Removing his old discoball ", v) v:Remove() ply["maxwyozi_discoball"] = (ply["maxwyozi_discoball"] or 1) - 1 end end end end) [/code] Although in my first post, I don't see why AnonTakesOver needed to replace v.SID and ply.SID with GetOwning_ent when it functions on DarkRP entities. IIRC, DarkRP automatically sets the entity's SID to the player's SID when they purchase it. I guess its like that so you don't have to create a data table inside each entity you add.
@insanekid2 This is the code I am using: [CODE]hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_DISC then for k, v in pairs(ents.FindByClass("sr_radio")) do if Isvalid(v.dt.owning_ent) and IsValid(ply) and v.dt.owning_ent == ply then v:Remove() end end end end)[/CODE] I really thought that was going to work but it did not, and as again there was no errors directing me to any problems!
[QUOTE=MoltoCraft;45390321]@insanekid2 This is the code I am using: [CODE]hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_DISC then for k, v in pairs(ents.FindByClass("sr_radio")) do if Isvalid(v.dt.owning_ent) and IsValid(ply) and v.dt.owning_ent == ply then v:Remove() end end end end)[/CODE] I really thought that was going to work but it did not, and as again there was no errors directing me to any problems![/QUOTE] Yeah, I believe that's due to using v.dt.owning_ent (same as GetOwning_ent). It requires there to have been a data table setup prior inside the entity. If your players are spawning this via DarkRP's F4 Menu, then I'd use this. [CODE]hook.Add("OnPlayerChangedTeam", "RemovingThosePrinters", function(ply, oldTeam) if oldTeam == TEAM_DISC then for k, v in pairs(ents.FindByClass("sr_radio")) do if IsValid(v.SID) and v.SID == ply.SID then v:Remove() end end end end)[/CODE]
@insanekid2 That didn't work either? Anyone got any other ideas!
[QUOTE=MoltoCraft;45395028]@insanekid2 That didn't work either? Anyone got any other ideas![/QUOTE] Show us the printers shared file?
Sorry, you need to Log In to post a reply to this thread.