• Thirdperson toggle
    19 replies, posted
So I am attempting to make a Deathrun server. I have a third person script here and I want to make it so it can be toggled. If I just throw this script in the init.lua as it is now it will always be toggled. I would like it to be able to be toggled by a player pressing F4. Also is it possible to edit this script so when players join the script is not toggled because when players join it is automatically toggled. I don't know much about coding in general so I am here to learn. Whether your answers work or not it will benefit me! :) I have also noticed that there are no tutorials on how to do a thing like this so hopefully this can benefit others. [CODE]function TPerson(ply, pos, angles, fov) local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end)[/CODE]
Just quickly wrote this, should work, I dunno. [lua]function SpectateScript( ply ) if ShallSpectate == 1 then ShallSpectate = 0 elseif ShallSpectate == 1 then ShallSpectate = 1 end TPerson() end hook.Add("ShowSpare2", "MySpectateScript", SpectateScript) function TPerson(ply, pos, angles, fov) if ShallSpectate == 1 then local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end)[/lua]
hmmmmm.... I don't see how a player would toggle this.
[QUOTE=Praesdynamite;45614564]hmmmmm.... I don't see how a player would toggle this.[/QUOTE] They press the F4 Menu. [lua] hook.Add("ShowSpare2", "MySpectateScript", SpectateScript) [/lua] The code above is the hook for the F4 menu which is also called ShowSpare2. You can also replace this to any of the other menu hooks. [lua] ShowHelp --F1 ShowTeam --F2 ShowSpare1 --F3 ShowSpare2 --F4 [/lua]
Oh! Thank you so much! EDIT: Not working.... [CODE][ERROR] gamemodes/deathrun/gamemode/cl_init.lua:609: 'end' expected (to close 'function' at line 585) near '<eof>' 1. unknown - gamemodes/deathrun/gamemode/cl_init.lua:0 Couldn't Load Init Script: 'deathrun/gamemode/cl_init.lua' [/CODE] The script goes in cl_init.lua correct?
[QUOTE=Praesdynamite;45614626]Oh! Thank you so much![/QUOTE] Anytime! Remember to mark your thread as solved once you have figured out the problem, though.
Um...... Would it go in init.lua? I will try that. EDIT: Putting it in init.lua does not work it breaks the whole server lol.
[lua]-- This in init.lua function SpectateScript( ply ) if ShallSpectate == 1 then ShallSpectate = 0 elseif ShallSpectate == 1 then ShallSpectate = 1 end umsg.Start( "MySpectationScript", ply ) umsg.End() end hook.Add("ShowSpare2", "MySpectateScript", SpectateScript) -- This in cl_init.lua function TPerson(ply, pos, angles, fov) if ShallSpectate == 1 then local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end) usermessage.Hook("MySpectationScript", TPerson)[/lua]
Will try. [editline]6th August 2014[/editline] [ERROR] gamemodes/deathrun/gamemode/cl_init.lua:598: 'end' expected (to close 'function' at line 575) near '<eof>' 1. unknown - gamemodes/deathrun/gamemode/cl_init.lua:0 Couldn't Load Init Script: 'deathrun/gamemode/cl_init.lua' Warning: Unhandled usermessage 'MySpectationScript' Warning: Unhandled usermessage 'MySpectationScript' Warning: Unhandled usermessage 'MySpectationScript' Everytime I hit F4 I get "Warning: Unhandled usermessade 'MySpectationScript' When I saved the second part in cl_init.lua I got the error...
Oh, right, I see. [lua] -- This in init.lua function SpectateScript( ply ) if ShallSpectate == 1 then ShallSpectate = 0 elseif ShallSpectate == 1 then ShallSpectate = 1 end umsg.Start( "MySpectationScript", ply ) umsg.End() end hook.Add("ShowSpare2", "MySpectationScript", SpectateScript) -- This in cl_init.lua function TPerson(ply, pos, angles, fov) if ShallSpectate == 1 then local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end) usermessage.Hook("MySpectationScript", TPerson)[/lua]
auggh This is frustrating... [ERROR] gamemodes/deathrun/gamemode/cl_init.lua:598: 'end' expected (to close 'function' at line 575) near '<eof>' 1. unknown - gamemodes/deathrun/gamemode/cl_init.lua:0 Couldn't Load Init Script: 'deathrun/gamemode/cl_init.lua' Seems something is still wrong with the cl_init.lua part.
[lua] I am so stupid some times. Hopefully this will work. -- This in cl_init.lua function TPerson(ply, pos, angles, fov) if not ShallSpectate == 1 local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end) usermessage.Hook("MySpectationScript", TPerson) [/lua]
[ERROR] gamemodes/deathrun/gamemode/cl_init.lua:577: 'then' expected near 'local' 1. unknown - gamemodes/deathrun/gamemode/cl_init.lua:0 [CODE]-- This in cl_init.lua function TPerson(ply, pos, angles, fov) if not ShallSpectate == 1 local then view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end) usermessage.Hook("MySpectationScript", TPerson)[/CODE] By the local I added in "then" Seems like the if then statement was not finished off.
Looks like I managed to fuck it up again :suicide: [lua] function TPerson(ply, pos, angles, fov) if not ShallSpectate == 1 then local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end) usermessage.Hook("MySpectationScript", TPerson) [/lua]
I'll use that instead [editline]6th August 2014[/editline] [ERROR] gamemodes/deathrun/gamemode/cl_init.lua:598: 'end' expected (to close 'function' at line 575) near '<eof>' 1. unknown - gamemodes/deathrun/gamemode/cl_init.lua:0 Couldn't Load Init Script: 'deathrun/gamemode/cl_init.lua' God why!!!!
For the first time ever I test something before I post it :v: Clientside: [lua] local ShallToggle = 0 function TPerson(ply, pos, angles, fov) if ShallToggle == 0 then local view = {} view.origin = pos-(angles:Forward()*100) + (angles:Right()*0) + (angles:Up()*10) view.angles = angles view.fov = fov return view end end hook.Add("CalcView", "TPerson", TPerson) hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end) concommand.Add("thirdperson_toggle", function(ply) if ShallToggle == 0 then ShallToggle = 1 elseif ShallToggle == 1 then ShallToggle = 0 end end) [/lua] Serverside [lua] hook.Add("ShowSpare2", "MyHook", function(ply) if CLIENT then ply:RunConsoleCommand("thirdperson_toggle") elseif SERVER then ply:ConCommand("thirdperson_toggle") end end) [/lua]
Well I did this. But another problem arises. When third person is toggled it works very well. Except when it is off i see the back of my head -_-....... idk what to do lol this hurts my head. On: [IMG]http://i.imgur.com/Lso4S67.jpg[/IMG] Off: [IMG]http://i.imgur.com/yEPfF35.jpg[/IMG]
[code]hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) return true end)[/code] this is the problem. Should be [code] hook.Add("ShouldDrawLocalPlayer", "ShouldDrawLocalPlayer", function(ply) if ShallToggle == 1 then return true end -- Don't do return ShallToggle == 1, it fucks up other mods using this hook end)[/code]
Makes my playermodel invisible except for the default gmod playermodel arm. [IMG]http://i.imgur.com/Z7nQ8pT.jpg[/IMG] When the third person is off I still see the back of my head. [editline]6th August 2014[/editline] After hours of scavaging the internet I found a working script. [url]http://facepunch.com/showthread.php?t=1288816[/url] I still wonder why that wasn't working.... Anyways thanks for everyone's help. I really appreciate it. I didn't expect you guys to go so far to help me :) Hope to see you guys around the forums soon! :wink:
[code]if ( CLIENT ) then local on = false -- default off local function toggle() on = !on if on == true then print( 'enabled' ) LocalPlayer():PrintMessage( HUD_PRINTTALK, "Third person mode enabled." ) else print( 'disabled') LocalPlayer():PrintMessage( HUD_PRINTTALK, "Third person mode disabled." ) end end hook.Add( "ShouldDrawLocalPlayer", "ThirdPersonDrawPlayer", function() if on and LocalPlayer():Alive() then return true end end ) hook.Add( "CalcView", "ThirdPersonView", function( ply, pos, angles, fov ) if on and ply:Alive() then local view = {} view.origin = pos - ( angles:Forward() * 70 ) + ( angles:Right() * 20 ) + ( angles:Up() * 5 ) view.angles = ply:EyeAngles() + Angle( 1, 1, 0 ) view.fov = fov return GAMEMODE:CalcView( ply, view.origin, view.angles, view.fov ) end end ) concommand.Add( "thirdperson_toggle", toggle ) end[/code] Assuming you have ulx and ulib put this in lua/ulx/modules/sh
Sorry, you need to Log In to post a reply to this thread.