• GetModel() and GetName() Return nil. Cant Find Vehicle name.
    17 replies, posted
I am trying to find the name of a vehicle that spawned (For example, when you remove a vehicle it shows the name of vehicle removed);however GetModel() and GetName() does not work, they always return nil on everything spawned. I Might also be doing this wrong because I want to know if a specific vehicle of mine has been spawned to fire some script stuff instead of fireing right when the user presses that spwn button. Code: [CODE]local Category = "ILoveCars" local Carname = "ThisIsATeSSSAAAASSst" local V = { // Required information Name = Carname, Class = "prop_physics", Category = Category, Model = "models/props_c17/oildrum001.mdl" // Optional information } hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced) function RecordPlaced( prop) if SERVER then PrintMessage(HUD_PRINTTALK, prop:GetModel()) end end list.Set( "Vehicles", "CarTest", V )[/CODE] I Tried with GetName() it didn't work either. Thanks for taking your time to review my problem. [B]EDIT:[/B] Solved! The last posts show how it was solved.
Move [code] hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced) [/code] to underneath [code] function RecordPlaced(prop) if SERVER then PrintMessage(HUD_PRINTTALK, prop:GetModel()) end end [/code]
[CODE]function RecordPlaced( prop) if SERVER then PrintMessage(HUD_PRINTTALK, prop:GetModel()) end end hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced)[/CODE] I had it like that before(And did it again now) no change. As for the error I get this: [QUOTE] [ERROR] lua/autorun/testcar.lua:23: bad argument #2 to 'PrintMessage' (string expected, got no value) 1. PrintMessage - [C]:-1 2. v - lua/autorun/testcar.lua:23 3. unknown - lua/includes/modules/hook.lua:84 4. Spawn - [C]:-1 5. DoPlayerEntitySpawn - gamemodes/sandbox/gamemode/commands.lua:247 6. GMODSpawnProp - gamemodes/sandbox/gamemode/commands.lua:162 7. unknown - gamemodes/sandbox/gamemode/commands.lua:20 8. unknown - lua/includes/modules/concommand.lua:54 [/QUOTE] [editline]22nd November 2015[/editline] I just set it so props ignite for 600 seconds, and it works(inside my record placed function) So its not the hook or function itself. [editline]22nd November 2015[/editline] THe model is also specified in the V table. So there is a model (And the fact that I can spawn a barrels from the vehicle section with my script proves so)
I think its beacouse youre putting the vehicle class with prop_physics. Change that to something like: vehicle_superawsome
Just did that, nothing changed. That happens on all entities, the hook is seperate from the table vehicle listing too. so just in general, GetName or GetModel returns nil on everything.
Are you on the latest gmod version or the developer version?
Latest gmod version from the steam store. (Legit copy)
Then this is very strange. Print an IsValid(prop) when the hook runs.
[CODE]function RecordPlaced( prop) print(IsValid(prop)) //PrintMessage(HUD_PRINTTALK,IsValid(prop)) prop:Ignite(600) //if SERVER then //PrintMessage(HUD_PRINTTALK, prop:GetModel()) //end end[/CODE] This prints true in the console. The odd thing it, it says "attempt to call method "Ignite" (A nil value) Yet it ignites my prop anyways [editline]22nd November 2015[/editline] Btw, prop:GetClass() works in my other script
[QUOTE=Tangyboxhead;49164018][CODE]function RecordPlaced( prop) print(IsValid(prop)) //PrintMessage(HUD_PRINTTALK,IsValid(prop)) prop:Ignite(600) //if SERVER then //PrintMessage(HUD_PRINTTALK, prop:GetModel()) //end end[/CODE] This prints true in the console. The odd thing it, it says "attempt to call method "Ignite" (A nil value) Yet it ignites my prop anyways [editline]22nd November 2015[/editline] Btw, prop:GetClass() works in my other script[/QUOTE] "attempt to call method "Ignite" (A nil value) is because the code is being run shared, it will error CLside and work correctly serverside maybe try doing [code]local foo = prop:GetModel() timer.Simple(1, function() PrintMessage(3, foo) end)[/code]
[CODE]function RecordPlaced( prop) print(IsValid(prop)) //PrintMessage(HUD_PRINTTALK,IsValid(prop)) //prop:Ignite(600) //PrintMessage(HUD_PRINTTALK, prop:GetClass()) //if SERVER then local foo = prop:GetModel() timer.Simple(1, function() PrintMessage(3, foo) end) //PrintMessage(HUD_PRINTTALK, prop:GetModel()) //end end hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced) [/CODE] IS that how it is supposed to look? It errors out even when I do an "if SERVER then" check
maybe it's trying to do it for every single entity possible
Youve commented the if SER VER then
[QUOTE=Splerge;49164071]maybe it's trying to do it for every single entity possible[/QUOTE] Nope, my other script(That works) uses the same hook. Only fires when a new entity makes itself present. [QUOTE=geferon;49164072]Youve commented the if SER VER then[/QUOTE] I know, even when it wasn't commented it would error out. Current script [CODE] function RecordPlaced( prop) print(IsValid(prop)) //PrintMessage(HUD_PRINTTALK,IsValid(prop)) //prop:Ignite(600) //PrintMessage(HUD_PRINTTALK, prop:GetClass()) if SERVER then local foo = prop:GetModel() timer.Simple(1, function() PrintMessage(3, foo) end) //PrintMessage(HUD_PRINTTALK, prop:GetModel()) end end hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced) local Category = "enny face" local Carname = "ThisIsATeSSSAAAASSst" local V = { // Required information Name = Carname, Class = "prop_physics", Category = Category, Model = "models/props_c17/oildrum001.mdl" // Optional information } list.Set( "Vehicles", "CarTest", V )[/CODE] Now with the Server check uncommented Error [QUOTE] [ERROR] lua/autorun/testcar.lua:11: bad argument #2 to 'PrintMessage' (string expected, got nil) 1. PrintMessage - [C]:-1 2. unknown - lua/autorun/testcar.lua:11 Timer Failed! [Simple][@lua/autorun/testcar.lua (line 10)] [/QUOTE] [editline]22nd November 2015[/editline] When I replace GetModel with GetClass it works, but GetModel returns nil. GetClass usualy says "prop_physics";however it says "Physics Prop" a bit odd but off topic. [editline]22nd November 2015[/editline] Odd, I might have fixed? Current script now [CODE] function RecordPlaced(prop) print(IsValid(prop)) //PrintMessage(HUD_PRINTTALK,IsValid(prop)) //prop:Ignite(600) //PrintMessage(HUD_PRINTTALK, prop:GetClass()) if SERVER then local foo = prop:GetClass() timer.Simple(1, function() PrintMessage(3, foo..prop:GetModel()) end) //PrintMessage(HUD_PRINTTALK, prop:GetModel()) end end hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced) local Category = "enny face" local Carname = "ThisIsATeSSSAAAASSst" local V = { // Required information Name = Carname, Class = "prop_physics", Category = Category, Model = "models/props_c17/oildrum001.mdl" // Optional information } list.Set( "Vehicles", "CarTest", V )[/CODE] It returns now [editline]22nd November 2015[/editline] It seems as if, even though its called after, the model data of a spawned prop isnt made present to lua until a few miliseconds later
Also you can fuck things up if the class of a car is prop_physics. Change it
[IMG]http://i.imgur.com/mgDsgdj.png[/IMG] [B] TL;DR [/B] I Fixed this problem with the help of many facepunchians here. The problem was, for some odd reason the "GetModel" data wasn't ready on entity creation, rather ready a few milliseconds after the entity was created. Using the a simple timer, i set it for a low value of .1( 100 milliseconds) of wait time for the data to get there. by the time the timer is done, the data should be here. I did the check I needed to, and thus was able to produce a cleaner correct script. [CODE] function RecordPlaced(prop) //CHECK IF IT IS SERVER INSTANCE, IF PROP EXISTS AND IF PROP IS VALID if SERVER and prop and (IsValid(prop)) then //BEGIN TIMER timer.Simple(.1, function() // NEW FUNCTION if prop:GetModel() then //CHECKS IF IT CAN GET MODEL OF THE VALID PROP DoCarStuff(prop) // IF IT CAN GET THE MODEL, THEN DO THIS end //END OF HOMEMADE FUNCTION end)//END TIMER end end // ONCE ALL NEEDED DATA HAS BEEN CONFIRMED, YOU CAN NOW MANIPULATE THIS MODEL TO YOUR HEARTS DESIRE. function DoCarStuff(tprop) PrintMessage(3, tprop:GetModel())// I just made it print the model name end // THIS DETECTS IF AN ENTITY HAS BEEN CREATED, THEN IT WILL SEND A HOOK TO RECORDPLACED AND DO WHAT IT NEEDS TO DO hook.Add("OnEntityCreated", "RecordPlacements", RecordPlaced) // THIS IS ALL RANDOM STUFF I MADE FOR TESTING local Category = "enny face" local Carname = "ThisIsATeSSSAAAASSst" local V = { // Required information Name = Carname, Class = "prop_physics", Category = Category, Model = "models/props_c17/oildrum001.mdl" // Optional information } list.Set( "Vehicles", "CarTest", V )[/CODE] I think the weird wait problem should be fixed, not sure if possible though. The script doesn't error, and only works if the prop is right. Thank you for your help!
Also that. It doesnt load after a few secs. You just have to make a timer. It xan be of 0.1 secs
Lol Ninja'd (Kinda) Also, I made it a prop_physics because I want to do a small project where I put a seat and weld automatically and spawn it. There is no set vehicle for this, and It doesn't(At least I hope so) mess with GMod
Sorry, you need to Log In to post a reply to this thread.