• Turning playermodel invisible on keypress
    5 replies, posted
Alright so I'm starting an addon and I want to make the users playermodel disappear on keypress I know I'd use either Player:KeyPressed or GM:KeyPress but I'm not sure which one? Also I know I'd put LocalPlayer():SetMaterial("") after that.
I would use GM.KeyPress and GM.KeyRelease for a hold and release trigger. If you want to toggle it, just use GM.KeyPress You probably want to do this server side, so disregard that LocalPlayer(). Additionally, setting the material to an empty string just defaults the entity back to its original material, so you probably want to make the player invisible by coloring them invisible, or not rendering them, there are multiple ways to approach this. [url]http://wiki.garrysmod.com/page/Entity/SetNoDraw[/url] or [url]http://wiki.garrysmod.com/page/Entity/SetRenderMode[/url] [url]http://wiki.garrysmod.com/page/Enums/RENDERMODE[/url] [url]http://wiki.garrysmod.com/page/Entity/SetColor[/url]
[QUOTE=McDunkable;50654679]I would use GM.KeyPress and GM.KeyRelease for a hold and release trigger. If you want to toggle it, just use GM.KeyPress You probably want to do this server side, so disregard that LocalPlayer(). Additionally, setting the material to an empty string just defaults the entity back to its original material, so you probably want to make the player invisible by coloring them invisible, or not rendering them, there are multiple ways to approach this. [url]http://wiki.garrysmod.com/page/Entity/SetNoDraw[/url] or [url]http://wiki.garrysmod.com/page/Entity/SetRenderMode[/url] [url]http://wiki.garrysmod.com/page/Enums/RENDERMODE[/url] [url]http://wiki.garrysmod.com/page/Entity/SetColor[/url][/QUOTE] I'll make it serverside soon. But for now I've got: [CODE]hook.Add("Tick", "test", function() if ply:KeyPressed(IN_DUCK) then print("It worked") ply:SetColor(255, 255, 255, 255) end end) [/CODE] where would I put the GM:KeyRelease? actually nevermind I think I got it [editline]6th July 2016[/editline] [QUOTE=Thane;50654713]I'll make it serverside soon. But for now I've got: [CODE]hook.Add("Tick", "test", function() if ply:KeyPressed(IN_DUCK) then print("It worked") ply:SetColor(255, 255, 255, 255) end end) [/CODE] where would I put the GM:KeyRelease? actually nevermind I think I got it[/QUOTE] Okay so whenever I press ctrl while pressing w, a, s, or d it briefly toggles on and of. Do you know of any way to fix that? EDIT: I went from using KeyPress to KeyDown. But now I have another problem. Whenever I'm in animation like reloading, jumping, or switching weapons, it rapidly flashes drawing my player and not. Here's the code [CODE] hook.Add("Tick", "test", function() if ply:KeyDown(IN_ATTACK2) then print("It worked") ply:SetNoDraw(true) else ply:SetNoDraw(false) end end) [/CODE] [editline]6th July 2016[/editline] So I've found a workaround to this problem. Here's what I've got [CODE] hook.Add("Tick", "test", function() if ply:GetActiveWeapon():GetPrintName() == weapon_crowbar then if ply:KeyDown(IN_ATTACK2) then print("It worked") ply:SetNoDraw(true) else ply:SetNoDraw(false) end end end) [/CODE] But it's not working for some reason when I hold the crowbar out or when I have any weapon out. I also tried putting weapon_crowbar in a string but that still didn't work. Also tried multiple weapons. Please help
[QUOTE=Shenesis;50655554][img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/Tick]GM:Tick[/url] does not return any player object so "ply" doesn't actually exist. Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerPostThink]GM:PlayerPostThink[/url] instead, which comes with a "ply" argument. Also, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/GetClass]Entity:GetClass[/url] instead of GetPrintName because the latter is used for display. Oh and, weapon_crowbar should be "weapon_crowbar" as it should be a string[/QUOTE] [CODE]hook.Add("PlayerPostThink", "test", function(ply) if ply:GetActiveWeapon():GetClass() == "weapon_crowbar" then if ply:KeyDown(IN_ATTACK2) then print("It worked") ply:SetNoDraw(true) else ply:SetNoDraw(false) end end end)[/CODE] this works however it doesn't matter what weapon I have out. EDIT: Nevermind I rejoined the game after realizing that I'd have to re-initiate the players think. And now it doesn't work at all.
Working Script: [CODE] hook.Add("Tick", "test", function() if LocalPlayer():GetActiveWeapon():GetClass() == "weapon_crowbar" then if LocalPlayer():KeyDown(IN_ATTACK2) then LocalPlayer():SetNoDraw(true) else LocalPlayer():SetNoDraw(false) end end end) [/CODE]
Sorry, you need to Log In to post a reply to this thread.