XP Update Help

I’m attempting to create a level/xp system for DarkRP. What I have so far in my script is working fine. I’m just having troubles with a serverside function.

I would like to make it so this function is always running, so it can check the next set of player experience needed.

The code works fine as a command, but I’m not quite sure on how to make this always running.



function LevelUp ( ply )
        if ply:GetLevel() == 1 then
            ply:SetPData("xpneeded", CIV_LEVEL_ONE_REQUIRED)
        else if ply:GetLevel() == 2 then
            ply:SetPData("xpneeded", CIV_LEVEL_TWO_REQUIRED)
        else
        return""
    end
end
end


Is there some sort of hook I can use?

Thanks

Edit:

I also tried to used a timer



timer.Create( "LevelCheck", 1, 0, LevelUp)


but I was given an error of





[ERROR] addons/darkrpmodification/lua/darkrp_modules/levelsystem/sv_levelsystem.lua:38: attempt to index global 'ply' (a nil value)
  1. unknown - addons/darkrpmodification/lua/darkrp_modules/levelsystem/sv_levelsystem.lua:38


Timer Failed! [LevelCheck][@addons/darkrpmodification/lua/darkrp_modules/levelsystem/sv_levelsystem.lua (line 48)]


You are using the timer wrong, read http://wiki.garrysmod.com/page/timer/Create

There’s one extra end in your LevelUp function.

I think your problem is that you have no idea what you are doing or how to program stuff.

The level up action should happen when you add experience, your code doesn’t make sense at all.

That’s not the level up action, it’s more of a needed exp check. I only named it that because I was working on the level up function but I got stuck here first.

Also no, I don’t know what I’m doing. That’s why I’m posting here asking for help.

Persious;
Thank you for the link. Reading up on proper timers at the moment.

I think there were at least two similar threads this week, you should search for them for easy answers/code.

The reason your code works when called as a console command is because console command takes a player caller argument (which you defined as “ply”), a timer doesn’t do this, however.

If you wish to run this on all players, set your code to:


function LevelUp ( )
for k, ply in pairs(player.GetAll()) do
        if ply:GetLevel() == 1 then
            ply:SetPData("xpneeded", CIV_LEVEL_ONE_REQUIRED)
        else if ply:GetLevel() == 2 then
            ply:SetPData("xpneeded", CIV_LEVEL_TWO_REQUIRED)
        else
        return""
    end
end
end


Otherwise, either add a filter or get player indexes by ID.

Thank you so much. Makes a lot of sense.