• Reward high karma with PS points.
    7 replies, posted
Hi, I am trying to get a script to work, that gives points when a players karma is between to numbers. My problem is, that I cant find a function to hook at to get the players karma. (also I cant find a script ) I thought about somthing like if karma >= num1 and karma <= num2 then PS.GivePoints(reward) if karma >= num3 and ..... Maybe somone already got one or can help me to bring mine alive. Thx Sitnalta
so you want someone to make you one???
Typically when you do something for higher karma, use [lua]if karma > x then... end // or else end [/lua] If you want to change rates for many; instead of using > and < to define, start at the highest first.. [lua]if ( karma > 1100 ) then elseif ( karma > 1000 ) then elseif ( karma > 900 ) then else end [/lua] The reason it works, is because it'll always go to the highest possible without falling through; if none are applicable then it'll go to else, you could omit the else, and add as many combinations as you want. It'll run quicker than two comparisons, but since it's O( 1 ) anyway, it won't matter by much unless you're really trying to prevent excess logic from running...
I know there is somthing wrong, but what. [CODE] if SERVER then AddCSLuaFile() num1 = 1 num2 = 2 num3 = 3 num4 = 4 Player = FindMetaTable("Player") if !Player then return end if TTTBeginRound() then if karma > 1800 then Player:PS_GivePoints(num1) elseif karma > 1600 then Player:PS_GivePoints(num2) elseif karma > 1400 then Player:PS_GivePoints(num3) elseif karma > 1200 then Player:PS_GivePoints(num4) else return end end hook.Add("TTTBeginRound", function() end) end[/CODE]
[lua] hook.Add("TTTEndRound","GivePointsToGoodPlayers",function() for k,v in pairs(player.GetAll()) do local karma = v:GetNWFloat("karma") if karma > 1100 then --Change it to your boundaries --Give them pointshop points v:PS_GivePoints(100) end end end) [/lua] I made this quickly for you, this is called when the round is over, and when its called, it gets all the players, and gets their karma and checks if it is over the threshold (which I put as 1100). If so it will run the code within the if statement. Hope it helps. (Ps, its a serverside file)
You only call the meta-table for an object when you want to extend it; here you're trying to use it as though it is the player when it's the object which contains player - but just because it's the object for player doesn't mean it is holding a copy... It's the template which is used when creating a player entity.
[QUOTE=Acecool;44648368]You only call the meta-table for an object when you want to extend it; here you're trying to use it as though it is the player when it's the object which contains player - but just because it's the object for player doesn't mean it is holding a copy... It's the template which is used when creating a player entity.[/QUOTE] Not to disrespect your way of teaching Ace, but to most people this won't mean much. You should try explaining it on a lower level. Think about it, if he's using meta-tables wrong, then he probably doesn't know how to use it, which means he doesn't know what it is/does. If you're going to explain, try to at least do it in a way that's beneficial for him.
Basically the FindMetaTable function returns the meta table for the class you specify - in this case, the player class. This is most commonly done to add a method onto the player class, eg: [lua]meta = FindMetaTable( "Player" ) -- We save the metatable as a variables function meta:getRank() -- The ":" specifies it is a new method, adding onto the metatable that we got earlier return self:GetNWString( "usergroup" ) -- The variable "self" is passed implicitly, it refers to the object the method is being used on end[/lua] This means that when we have a player object, we can run this method on them: [lua]local ply = player.GetByID( 1 ) -- Get the first player in the server print( ply:getRank() ) -- Print their rank that is returned with the function I defined earlier [/lua] What you're trying to do is run something on every player on the server. You need to iterate over all the player objects. player.GetAll() is a function which returns a table of all the player objects in the server, allowing you to iterate over them and do whatever you want on each (generally done in a for loop using the pairs iterator, which returns both the key and value every iteration): [lua] for k, ply in pairs( player.GetAll() ) do -- k = key, ply = player object print( ply:getRank() ) end [/lua]
Sorry, you need to Log In to post a reply to this thread.