ULX

Author Topic: Trying to learn lua a little  (Read 5631 times)

0 Members and 2 Guests are viewing this topic.

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Trying to learn lua a little
« on: February 12, 2014, 08:40:53 PM »
So I am trying to learn lua a little and I wrote this for my server.

Code: [Select]
function AddHpToLevel(ply)
if ( ply:getLevel() <= 60 ) then
ply:SetHealth(700)
end
if ( ply:getLevel() <= 50 ) then
ply:SetHealth(600)
end
if ( ply:getLevel() <= 40 ) then
ply:SetHealth(500)
end
if ( ply:getLevel() <= 30 ) then
ply:SetHealth(400)
end
if ( ply:getLevel() <= 20 ) then
ply:SetHealth(300)
end
if ( ply:getLevel() <= 10 ) then
ply:SetHealth(200)
end
end
hook.Add("PlayerSpawn", "test", AddHpToLevel)

But it doesn't seem to work. Is there something I am doing wrong?

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Trying to learn lua a little
« Reply #1 on: February 12, 2014, 08:59:09 PM »
Where do you have the file this code is in?
Tried adding print messages for debugging, both within the function, and outside of it (to see that it's even loading?)
Do you have a function called getLevel somewhere?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #2 on: February 12, 2014, 09:02:54 PM »
Ok it does run but doesn't set my HP. The getLevel is this function.
Code: [Select]
function meta:getLevel()
return self:getDarkRPVar('level')
end

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Trying to learn lua a little
« Reply #3 on: February 12, 2014, 09:07:06 PM »
Okay...

Does getDarkRPVar exist?
Out of the Garry's Mod business.

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #4 on: February 12, 2014, 09:11:12 PM »
Okay...

Does getDarkRPVar exist?

Yes, the level mod works fine.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Trying to learn lua a little
« Reply #5 on: February 12, 2014, 09:28:39 PM »
Perhaps you could try removing the quotes from
Code: [Select]
self:getDarkRPVar('level')
Out of the Garry's Mod business.

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #6 on: February 12, 2014, 09:38:40 PM »
Perhaps you could try removing the quotes from
Code: [Select]
self:getDarkRPVar('level')

The level mod works fine, it is just that I want it to set a users health depending on what level, aka level 60 gets 600 health. I am running a Gang Wars RP so the more health the better

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Trying to learn lua a little
« Reply #7 on: February 12, 2014, 10:07:32 PM »
How about instead of end...

Start using elseif, right now if you have it so any level below 60 gets 700 HP.
Out of the Garry's Mod business.

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #8 on: February 13, 2014, 07:36:39 AM »
Ok I did that but it is still not setting HP. It does run the script though.
Code: [Select]
function AddHpToLevel(ply)
if ( ply:getDarkRPVar('level') >= 60 ) then
ply:SetHealth(700)
elseif ( ply:getDarkRPVar('level') >= 50 ) then
ply:SetHealth(600)
elseif ( ply:getDarkRPVar('level') >= 40 ) then
ply:SetHealth(500)
elseif ( ply:getDarkRPVar('level') >= 30 ) then
ply:SetHealth(400)
elseif ( ply:getDarkRPVar('level') >= 20 ) then
ply:SetHealth(300)
elseif ( ply:getDarkRPVar('level') >= 10 ) then
ply:SetHealth(200)
end
end
hook.Add("PlayerSpawn", "test", AddHpToLevel)

print( "Loaded HP Gen2222");
I have tried that with no luck.
« Last Edit: February 13, 2014, 07:40:14 AM by Desimay »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Trying to learn lua a little
« Reply #9 on: February 13, 2014, 07:58:15 AM »
1) As I originally asked, where are you putting this code?
It's possible, depending on where it is, that it's loading before the functions it uses are created by the gamemode or addon.
2) Try a print command inside the function too, to see if it's working right when it runs, if the hook is even running.
Code: [Select]
function AddHpToLevel(ply)
print ( ply:Nick() .. " level is ".. ply:getDarkRPVar('level') )
if ( ply:getDarkRPVar('level') >= 60 ) then
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #10 on: February 13, 2014, 07:59:54 AM »
1) As I originally asked, where are you putting this code?
It's possible, depending on where it is, that it's loading before the functions it uses are created by the gamemode or addon.
2) Try a print command inside the function too, to see if it's working right when it runs, if the hook is even running.
Code: [Select]
function AddHpToLevel(ply)
print ( ply:Nick() .. " level is ".. ply:getDarkRPVar('level') )
if ( ply:getDarkRPVar('level') >= 60 ) then

I did some testing and I ran this on my dev server
Code: [Select]
lua_run_cl for k,v in pairs(player.GetAll() ) do local lvl = v:getDarkRPVar('level') print(lvl) end  and that seems to work. So getting the level is client side but I need to set the health.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Trying to learn lua a little
« Reply #11 on: February 13, 2014, 08:02:17 AM »
Does the darkrp level var or getlevel function exist on server?
Seems silly the info like that would be client only.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #12 on: February 13, 2014, 08:04:54 AM »
Does the darkrp level var or getlevel function exist on server?
Seems silly the info like that would be client only.
The getDarkRPVar('level') is client side.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Trying to learn lua a little
« Reply #13 on: February 13, 2014, 08:40:46 AM »
Right, seeing 'self' in the actual function indicates that the function itself is client.
You'd need to find out if the DarkRP variable 'level' exists on the server side.
If it's on the server, a server side function could be written (if not already exist) to get the value for players on server side.
(return ply:getDarkRPVar('level') instead of self:)

EDIT: According to DarkRP WIKI - GetDarkRPVar is shared.
http://wiki.darkrp.com/index.php/Functions/player/shared/getdarkrpvar
You just need to make sure your code is running on server side only.
« Last Edit: February 13, 2014, 08:47:09 AM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Desimay

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Trying to learn lua a little
« Reply #14 on: February 13, 2014, 11:17:54 AM »
Right, seeing 'self' in the actual function indicates that the function itself is client.
You'd need to find out if the DarkRP variable 'level' exists on the server side.
If it's on the server, a server side function could be written (if not already exist) to get the value for players on server side.
(return ply:getDarkRPVar('level') instead of self:)

EDIT: According to DarkRP WIKI - GetDarkRPVar is shared.
http://wiki.darkrp.com/index.php/Functions/player/shared/getdarkrpvar
You just need to make sure your code is running on server side only.

Ok I think I found something that might work.

Client Side:
Code: [Select]
if ( SERVER ) then return; end

function PlayerCheck()
for k,v in pairs( player.GetAll() ) do
if ( v:getDarkRPVar('level') >= 70 ) then
net.Start("Send_HP_70")
net.SendToServer()
elseif ( v:getDarkRPVar('level') >= 60 ) then
net.Start("Send_HP_60")
net.SendToServer()
elseif ( v:getDarkRPVar('level') >= 50 ) then
net.Start("Send_HP_50")
net.SendToServer()
elseif ( v:getDarkRPVar('level') >= 40 ) then
net.Start("Send_HP_40")
net.SendToServer()
elseif ( v:getDarkRPVar('level') >= 30 ) then
net.Start("Send_HP_30")
net.SendToServer()
elseif ( v:getDarkRPVar('level') >= 20 ) then
net.Start("Send_HP_20")
net.SendToServer()
elseif ( v:getDarkRPVar('level') >= 10 ) then
net.Start("Send_HP_10")
net.SendToServer()
end
end
end
hook.Add( "PlayerSpawn", "PlayerCheck", PlayerCheck);

Server Side:
Code: [Select]
if ( CLIENT ) then return; end

util.AddNetworkString( "Send_HP_70" );
util.AddNetworkString( "Send_HP_60" );
util.AddNetworkString( "Send_HP_50" );
util.AddNetworkString( "Send_HP_40" );
util.AddNetworkString( "Send_HP_20" );
util.AddNetworkString( "Send_HP_10" );

net.Receive( "Send_HP_70", function(ply)
ply:SetHealth(700);
end )

net.Receive( "Send_HP_60", function(ply)
ply:SetHealth(700);
end )

net.Receive( "Send_HP_50", function(ply)
ply:SetHealth(700);
end )

net.Receive( "Send_HP_40", function(ply)
ply:SetHealth(700);
end )

net.Receive( "Send_HP_30", function(ply)
ply:SetHealth(700);
end )

net.Receive( "Send_HP_20", function(ply)
ply:SetHealth(700);
end )

net.Receive( "Send_HP_10", function(ply)
ply:SetHealth(700);
end )