I'm trying to spawn an entity where the player is looking, here's what I've got
[code]
hook.Add("KeyPress", "test3", function(ply, key)
if (key == IN_RELOAD) then
if ply:GetActiveWeapon():IsValid() then
if ply:GetActiveWeapon():GetClass() == "weapon_crowbar" then
local ent2 = ents.Create("npc_citizen")
local ent = ply:GetEyeTrace().Entity
ent2:SetModel(ply:GetModel())
ent2:SetPos(ply:EyePos())
end
end
end
end)
[/code]
the code works perfectly fine although it spawns the entity where the players playermodels eyes are at, if that makes sense. I don't know how to get the position of where the player is looking. I checked the wiki and couldn't find anything. Thanks.
ply:GetEyeTrace().HitPos for the position of where the player is looking at. Where on wiki have you looked for?
[QUOTE=Robotboy655;50688512]ply:GetEyeTrace().HitPos for the position of where the player is looking at. Where on wiki have you looked for?[/QUOTE]
I looked in the Entity and Player Classes and couldn't find anything. I'll try this though, thanks Robotboy.
EDIT: Yeah this worked thanks! One more question, how would I make a timer that despawns the entity after say 5 seconds?
Is it too much of a bother for you sir to type timer into the search bar on the wiki? Or "timer gmod" into google or the address bar of your web browser?
[QUOTE=Robotboy655;50688588]Is it too much of a bother for you sir to type timer into the search bar on the wiki? Or "timer gmod" into google or the address bar of your web browser?[/QUOTE]
That part isn't the problem, I don't know how to despawn the entity.
EDIT:
You're right this was dumb I apologize.
[QUOTE=Thane;50688592]That part isn't the problem, I don't know how to despawn the entity.[/QUOTE]
That, too, can be found via the wiki and google.
[QUOTE=man with hat;50688661]That, too, can be found via the wiki and google.[/QUOTE]
Well that was easy. Sorry for the dumb questions.
[code]
hook.Add("KeyPress", "test3", function(ply, key)
if (key == IN_RELOAD) then
if ply:GetActiveWeapon():IsValid() then
if ply:GetActiveWeapon():GetClass() == "weapon_crowbar" then
local ent2 = ents.Create("npc_citizen")
local ent = ply:GetEyeTrace().Entity
ent2:SetModel(ply:GetModel())
ent2:SetPos(ply:GetEyeTrace().HitPos)
timer.Simple(5, function () ent2:Remove() end)
print (ply:GetEyeTrace().HitPos)
end
end
end
end)
[/code]
I suggest localizing the active weapon and using only 1 if statement, for example.
[code]
local Weapon = Player:GetActiveWeapon()
if ( IsValid( Weapon ) and Weapon:GetClass() == "weapon_crowbar" ) then
...
end
[/code]
You should also be using the global function IsValid instead of calling the entity method IsValid, since if the entity is nil, you'll get a Lua error. The global function IsValid basically does this...
[code]
return a and a.IsValid and a:IsValid()
[/code]
Finally, you should check if "ent2" is valid before removing it, once again, to prevent a Lua error. Generally whenever you deal with delayed callbacks, you should check if everything is still valid / as it should be.
Sorry, you need to Log In to post a reply to this thread.