Hello everyone
Ok since summer just hit for me I decided to make a custom SWep for my server. I am doing this to help me learn lua and I have looked at some lua tutorials but I am still uncertain about somethings. The gun I am trying to make would simply shrink the player your aiming at for v 1.0. I would like the gun to shrink the player I am aiming at but I am confused on how this could be achieved. I am guessing something along the means like "while this unique ID is being target then do SetModelScale(i, 0) *Yes I know there is a I variable, that is for it to shrink over time as long as I am aiming at them*". I have managed to figure out how to change my own player models scale but the only issue now is the camera view constantly resets back to its normal scaled position. I have seen a ton of eyepos() and related commands. The one problem with that is I do not know which one to use. THe one thing I have also seen is that SetModelScale does not scale down jiggle bones. The last thing I am having a issue with is when my server starts it shrinks my player model. I have checked my lua/autorun folder and there is nothing in there that should be doing that.
I use this to look up commands: [url]http://glua.me/search/[/url]
This is just a picture of one of the issues I am facing right now:
[IMG]http://i607.photobucket.com/albums/tt152/bran92don/nopeavi/2013-05-07_00002_zpsf9f0c521.jpg[/IMG]
As you can see the view is not positioned correctly.
Here is some of the code:
[CODE]i=1.0
x=.1
function SWEP:PrimaryAttack()
i= i - x
self.Weapon:EmitSound(Sound("weapons/ar2/npc_ar2_altfire.wav"))
self:ShootBullet( 100, 1, 0 )
if i <= 0.1 then
i=0.3
end
self.Owner:SetModelScale(i, 3)
self.Owner:ViewPunch( Angle( -1, 0, 0 ) )
if ( !self:CanPrimaryAttack() ) then return end
self.Weapon:EmitSound( self.Primary.Sound )
self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
self:TakePrimaryAmmo( 1 )
self.Weapon:SetNextPrimaryFire( CurTime() + .2 )
local trace = self.Owner:GetEyeTrace()
if self.Weapon:Clip1() <= 0 then
self:Reload()
return
end
end
[/CODE]
Thanks in advance
P.S. If you guys know of some like project list that I could do daily that would be awesome. I learn best by doing and I rather have like some project to do to help me learn.
Can anyone help me on this? I thought it was something pretty simple?
So let me get this straight:
You want a swep that shrinks people.
You've been able to shrink people.
You can't seem to lower their eye position to the shrunk eye pos.
Anything else before I try to help?
[QUOTE=bobbleheadbob;40578374]So let me get this straight:
You want a swep that shrinks people.
You've been able to shrink people.
You can't seem to lower their eye position to the shrunk eye pos.
Anything else before I try to help?[/QUOTE]
Yes, except that I have only been able to shrink my own player model. I have not tested it on other players. I am guessing you would put somewhere in the code to get the unique ID of the player your looking at and then tell it to shrink that ID's player model. You pretty much understand what I need help with though.
You wouldn't have to use their userid. Just umsg.Entity(ply) and use it clientside.
like this:
[LUA]
--ON SERVER:
umsg.Start("SendShrink")
umsg.Entity(ply)
umsg.Float(shrinkamount)
umsg.End()
--ON CLIENT:
usermessage.Hook("SendShrink", function(um)
local ply = um:ReadEntity()
local amt = um:ReadFloat()
--Shrink them here...
end)
[/LUA]
because model scale is clientside, do everything serverside and then just send the shrink amount to the clients.
As for the eye pos...
[url=http://glua.me/search/?keywords=Player.SetViewOffset][img]http://glua.me/search.png[/img] [b]Player.SetViewOffset[/b][/url]
[url]http://gmodwiki.net/Lua/Classes/Player/SetViewOffset[/url]
so...
[LUA]
usermessage.Hook("SendShrink", function(um)
local ply = um:ReadEntity()
local amt = um:ReadFloat()
--Shrink them here...
if ply == LocalPlayer() then
--Find a way to calculate the relation between shrink amount and head position, then...
LocalPlayer():SetViewOffset(Vector(0,0,offset))
end
end)
[/LUA]
[QUOTE=bobbleheadbob;40578914]You wouldn't have to use their userid. Just umsg.Entity(ply) and use it clientside.
like this:
[LUA]
--ON SERVER:
umsg.Start("SendShrink")
umsg.Entity(ply)
umsg.Float(shrinkamount)
umsg.End()
--ON CLIENT:
usermessage.Hook("SendShrink", function(um)
local ply = um:ReadEntity()
local amt = um:ReadFloat()
--Shrink them here...
end)
[/LUA]
because model scale is clientside, do everything serverside and then just send the shrink amount to the clients.
As for the eye pos...
[url=http://glua.me/search/?keywords=Player.SetViewOffset][img]http://glua.me/search.png[/img] [b]Player.SetViewOffset[/b][/url]
[url]http://gmodwiki.net/Lua/Classes/Player/SetViewOffset[/url]
so...
[LUA]
usermessage.Hook("SendShrink", function(um)
local ply = um:ReadEntity()
local amt = um:ReadFloat()
--Shrink them here...
if ply == LocalPlayer() then
--Find a way to calculate the relation between shrink amount and head position, then...
LocalPlayer():SetViewOffset(Vector(0,0,offset))
end
end)
[/LUA][/QUOTE]
Thanks for the help. What is funny is that "Player:SetViewOffset(Vector viewOffset)" Was the first thing I tried.
Also i don't really want to be just given the code I really would like to know what exactly everything is doing as well. That is the only way I will be able to learn. I hope that is not a bother, if so I will just keep experimenting until I can understand every nick and cranny. I just would prefer not to waste to much time on this one thing because to me it seems like something a beginner should be able to do.
Allow me to clarify with comments...
[LUA]
if SERVER then --check to see if this is the server or the client. usermessages, or umsg's, can only be sent by the server to the clients.
umsg.Start("SendShrink")--This starts a usermessage. Basically a console command that the server sends to the clients. The usermessage will trigger a function on the client's computer. We'll call it "SendShrink", which basically serves as the function name. By not including a second argument to umsg.Start(), we send the usermessage to ALL players, which we want to do in this case.
umsg.Entity(ply)--this adds the player entity as a part of that usermessage. This entity will be received by the recipient and the client will know who to shrink.
umsg.Float(shrinkamount)--this adds the shrink amount to the usermessage. We'll use this later as well.
umsg.End()--Should be called umsg.SEnd. Basically sends the usermessage to all the recipients.
end
--I should mention that this would probably go into SWEP:PrimaryAttack(). You would use your current code to find out how much to shrink by, but instead of applying the shrink, use this above code.
--The following would go OUTSIDE of anything. Probably underneath SWEP:PrimaryAttack(). Don't put it INSIDE PrimaryAttack, because then it won't be ran.
if CLIENT then--back to the clientside.
usermessage.Hook("SendShrink", function(um)--much like concommand.Add, usermessage.Hook is used to listen for usermessages from the server. Any time the client recieves a packet called "SendShrink", it runs the function you put in there. to save space, I've placed the function WITHIN the usermessage.Hook paramaters. Another way of doing this would be to create a function and then below that function do usermessage.Hook("SendShrink", FunctionName). The um argument in usermessage hooks contains the data we sent earlier on the server. The data is encrypted, though, so we need to read it...
local ply = um:ReadEntity()--This gets the player we're talking about from the usermessage.
local amt = um:ReadFloat()--This gets the amount to set the shrinkage to.
--It's best to assign variables to each item in the usermessage right off the bat instead of constantly referring to them as um:ReadFloat(). I'm not SURE, but I'm fairly certain that if you read objects in the wrong order or more than once it will pop an error, so it's best to just make local variables in the order that the data was added to the usermessage.
--Shrink them here. Put your code for shrinking players into this space, using ply for the target and amt for the amount to shrink them by.
if ply == LocalPlayer() then --This will check to see if the person being shrunk is the client, or the person receiving the usermessage. The reason we do this is because we don't need to set the player's view offset for EVERYONE.
--Find a way to calculate the relation between shrink amount and head position, then...
--I'm not quite sure what the math here would be. Try 64, the default, times the shrink decimal. That would make sense.
ply:SetViewOffset(Vector(0,0,offset))--offset would be that number you arrived at right here^
end
end)
end
[/LUA]
I hope that made things a bit more clear. :) Any more questions?
[QUOTE=bobbleheadbob;40584540]Allow me to clarify with comments...
[LUA]
if SERVER then --check to see if this is the server or the client. usermessages, or umsg's, can only be sent by the server to the clients.
umsg.Start("SendShrink")--This starts a usermessage. Basically a console command that the server sends to the clients. The usermessage will trigger a function on the client's computer. We'll call it "SendShrink", which basically serves as the function name. By not including a second argument to umsg.Start(), we send the usermessage to ALL players, which we want to do in this case.
umsg.Entity(ply)--this adds the player entity as a part of that usermessage. This entity will be received by the recipient and the client will know who to shrink.
umsg.Float(shrinkamount)--this adds the shrink amount to the usermessage. We'll use this later as well.
umsg.End()--Should be called umsg.SEnd. Basically sends the usermessage to all the recipients.
end
--I should mention that this would probably go into SWEP:PrimaryAttack(). You would use your current code to find out how much to shrink by, but instead of applying the shrink, use this above code.
--The following would go OUTSIDE of anything. Probably underneath SWEP:PrimaryAttack(). Don't put it INSIDE PrimaryAttack, because then it won't be ran.
if CLIENT then--back to the clientside.
usermessage.Hook("SendShrink", function(um)--much like concommand.Add, usermessage.Hook is used to listen for usermessages from the server. Any time the client recieves a packet called "SendShrink", it runs the function you put in there. to save space, I've placed the function WITHIN the usermessage.Hook paramaters. Another way of doing this would be to create a function and then below that function do usermessage.Hook("SendShrink", FunctionName). The um argument in usermessage hooks contains the data we sent earlier on the server. The data is encrypted, though, so we need to read it...
local ply = um:ReadEntity()--This gets the player we're talking about from the usermessage.
local amt = um:ReadFloat()--This gets the amount to set the shrinkage to.
--It's best to assign variables to each item in the usermessage right off the bat instead of constantly referring to them as um:ReadFloat(). I'm not SURE, but I'm fairly certain that if you read objects in the wrong order or more than once it will pop an error, so it's best to just make local variables in the order that the data was added to the usermessage.
--Shrink them here. Put your code for shrinking players into this space, using ply for the target and amt for the amount to shrink them by.
if ply == LocalPlayer() then --This will check to see if the person being shrunk is the client, or the person receiving the usermessage. The reason we do this is because we don't need to set the player's view offset for EVERYONE.
--Find a way to calculate the relation between shrink amount and head position, then...
--I'm not quite sure what the math here would be. Try 64, the default, times the shrink decimal. That would make sense.
ply:SetViewOffset(Vector(0,0,offset))--offset would be that number you arrived at right here^
end
end)
end
[/LUA]
I hope that made things a bit more clear. :) Any more questions?[/QUOTE]
Hey thanks for all your help, sorry I didn't respond earlier I have just been busy. ALso I have been thinking shouldn't this code be in a like autorun lua file. I only ask because if the code to shrink the players is in the swep, how would I shrink someone who doesn't have the swep. It seems like to me If they do not have the code to run it it would do nothing to them, but I am guessing the code in sweps is automatically ran on all clients regardless if they have it since nothing was mentioned though.
Edit:
Scratch that it works now. when I first tried the code it kept returning a nil value. I don't know if it was something I did to fix it but its working now.
Yep. It would all go into the SWEP's files.
The first bit would go into SWEP:PrimaryAttack(), everything after that would go wherever. All weapons are loaded on all clients and the server at game initialize.
Sorry, you need to Log In to post a reply to this thread.