Ulysses
General => Developers Corner => Topic started by: Desimay on February 12, 2014, 08:40:53 PM
-
So I am trying to learn lua a little and I wrote this for my server.
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?
-
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?
-
Ok it does run but doesn't set my HP. The getLevel is this function.
function meta:getLevel()
return self:getDarkRPVar('level')
end
-
Okay...
Does getDarkRPVar exist?
-
Okay...
Does getDarkRPVar exist?
Yes, the level mod works fine.
-
Perhaps you could try removing the quotes from
self:getDarkRPVar('level')
-
Perhaps you could try removing the quotes from
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
-
How about instead of end...
Start using elseif, right now if you have it so any level below 60 gets 700 HP.
-
Ok I did that but it is still not setting HP. It does run the script though.
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.
-
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.
function AddHpToLevel(ply)
print ( ply:Nick() .. " level is ".. ply:getDarkRPVar('level') )
if ( ply:getDarkRPVar('level') >= 60 ) then
-
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.
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
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.
-
Does the darkrp level var or getlevel function exist on server?
Seems silly the info like that would be client only.
-
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.
-
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 (http://wiki.darkrp.com/index.php/Functions/player/shared/getdarkrpvar)
You just need to make sure your code is running on server side only.
-
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 (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:
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:
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 )
-
That is WAY more complicated than it needs to be. Also it won't work at all. PlayerSpawn is serverside, and why are you looping through every player, what in the world are you doing??
This is literally all you need:
hook.Add( "PlayerSpawn", "PlayerCheck", function( ply )
local lv = ply:getDarkRPVar( "level" )
if lv > 10 then
ply:SetHealth( lv * 10 )
end
end )
-
Indeed, you could have kept your original code, just added a bit.
There are two ways to make sure your code is on server side.
1) /autorun/cl or /sv - obvious where.
OR
2) You used part of the idea already, but then, instead of going from from point A to point be in a straight line, you circumnavigated the globe going the same direction.
Simple use your code, but add like this
if SERVER then
... your original code ...
end
I've not tested your code, nor do I know DarkRP to know it will work, but, one step at a time, keep it simple, get it 'running' on server only.