• Do you know if there is a way to Detect a specific entity with GetEyeTrace() ?
    13 replies, posted
-Hey, i'm new to Lua and I was wondering if there is a way to Detect if you're looking at a specific Entity, like a specific props (The green couch for example ) and set this value to a variable and then test it with GetEyeTrace(). I'm trying to make a script that if you look at the green couch it Kill you or something like that. (Btw I would like to post the beginning of my script but I can't find how to add code lines) Thank you for reading my thread.
https://files.facepunch.com/forum/upload/106967/641f7712-8d86-4448-964d-3559c17936e8/chrome_2018-01-27_19-42-31.png if eyeEntity:GetModel() == "coach_model" then DoSomething() end
See: TraceResult Structure and GetEyeTrace
This skips the trace results and gets the entity from it. If you're not looking at an ent then it won't run. Not much, but it will be a start for you concommand.Add("eyetrace", function(pPlayer)     local ent = pPlayer:GetEyeTrace().Entity     if not ent then return end          if ent:GetModel() == "mycouch.mdl" then         MsgN("Hey guys! I found a couch.")     end end)
You should check if the entity is valid before calling GetModel, not a nil check like you're currently doing.
Thanks for the help! I finally found a solution thanks to you. I do not know how I couldn't saw it, even so I checked and double checked several times to see if there wasn't a function to add lines of code. Here's the code: hook.Add("Think" , "Test", function ()  entity = LocalPlayer():GetEyeTrace().Entity if entity:GetModel() == "models/props_c17/furniturecouch002a.mdl" then   print("You have found the green couch") end end) (Obviously print "You have found the green couch" when you specifically look at it) Do you know by what I could replace "LocalPlayer()" to avoid to run it on the client ?
If you mean to run it on the server, it depends on which players you want to run it on. You can use a GM/PlayerTick hook, instead if you want it to run for every player.
I tried this: hook.Add(GAMEMODE:PlayerTick(Ply,Cmv )  , "Test", function () entity = Ply:GetEyeTrace().Entity  if IsValid(entity) and entity:GetModel() == "models/props_c17/furniturecouch002a.mdl" then   Ply:ChatPrint("You have seen the Couch")  end end) But it doesn't seems to work.
You use the function name as the hook event name, just like "Think".
I'm sorry but I don't understand.
"Think" is the event name for GM:Think, so what would the event name be for GM:PlayerThink?
Thanks for the help ! I'm new to lua and coding as well. The valid code if anyone want: hook.Add("PlayerTick", "Test", function (Ply,cvm) entity = Ply:GetEyeTrace().Entity  if IsValid(entity) and entity:GetModel() == "models/props_c17/furniturecouch002a.mdl" then   Ply:ChatPrint("You have seen the Couch")  end end)
Make sure to make your "entity" variable local!
It should be perfect like this !
Sorry, you need to Log In to post a reply to this thread.