Jump to content

solved


Recommended Posts

  • Moderators

Everything is explained with commentaries:

local reputations = { 
    [-1095] = "demon", 
    [-195]  = "evil", 
    [-55]   = "bad", 
    [0]     = "normal", 
    [55]    = "good1", 
    [195]   = "good2", 
    [1095]  = "hero" 
} 
  
-- this function will return the reputation name giving him the  reputation points 
-- i.e getReputationName(-120) will return "evil" 
function getReputationName( points ) 
    for reppoints, repname in ipairs ( reputations ) do -- loop over all reputations 
        result = repname 
        --stop the loop if the rep from the table is higher than the rep of the player 
        if reppoints > points then 
            return result --we found it, so let's return it 
        end 
    end 
end 
  
-- this function will update the reputation name of the player 
-- if the player has -120, getElementData on "Reputation.status" will return "evil" 
function updateReputationName( player ) 
    local repPoints = getElementData(killer, "Reputation.points") or 0 
    local newRepName = getReputationName( repPoints ) 
    setElementData(player, "Reputation.status", newRepName) 
end 
  
function reputation(ammo, killer, weapon, bodypart) 
    if killer and killer ~= source then 
        --Reputation points of the guy who died 
        local dRPoints = getElementData(source, "Reputation.points") or 0 
  
        --Reputation points of the guy who killed him 
        local kRPoints = getElementData(killer, "Reputation.points") or 0 
  
        if dRPoints < 0 then -- the guy who died was a bad guy 
            kRPoints = kRPoints + 5 
        else --was a good guy 
            kRPoints = kRPoints - 5 
        end 
        setElementData(killer, "Reputation.points",  
        updateReputationName( killer ) -- update the reputation name for the killer 
    end 
end 
addEventHandler( "onPlayerWasted", getRootElement(), reputation) 
  
function onLogin (_, account) 
    local defaultPoints = 0 --you can change the default points a guy should have here 
    local defaultRepName = getReputationName( defaultPoints ) --do not modify here 
    setElementData(source, "Reputation.status", getAccountData(account, "rep") or defaultRepName) 
    setElementData(source, "Reputation.points", getAccountData(account, "re.p") or defaultPoints) 
end 
addEventHandler ("onPlayerLogin", root, onLogin) 
  
function saveData(thePlayer, theAccount) 
    if (theAccount and not isGuestAccount(theAccount)) then 
        setAccountData (theAccount, "rep", getElementData(thePlayer, "Reputation.status")) 
        setAccountData (theAccount, "re.p", getElementData(thePlayer, "Reputation.points")) 
    end 
end 
  
addEventHandler ("onPlayerQuit", root, function () saveData(source, getPlayerAccount(source)) end) 
addEventHandler ("onPlayerLogout", root, function (prev) saveData(source, prev) end) 

I didn't test it so if it works, please post the errors if there is any.

Link to comment
  • Moderators
Everything is explained with commentaries

That's besides the point, though. I'm here to help, but I'm not gonna read through it all and try to spot mistakes when simply providing the errors and/or warnings would make it easier for us all ;)

You are totally right ! By the way this script has only 30 lines so yeah I just worked into it even if he didn't post the errors/warning.

The rules are here but no one read it so yeah, we (moderators) should probably start to lock a topic untill it's ok.

What information should my question contain?

- What is the reason you make new topic? Whether it's problem with your script or need help with something else.

- Explain what is wrong? Tell us what happens when you test your script and what is that you want it to do.

- Give us some of your code so that we can help you correct it.

- Tell us if you get any error or warning messages and what they are. We can determine what causes the script not to work quicker.

Link to comment
  • 2 weeks later...
local reputations = { 
    [-1095] = "demon", 
    [-795]  = "evil", 
    [-10]   = "bad2", 
    [-5]   = "bad1", 
    [0]     = "normal", 
    [5]    = "good1", 
    [10]   = "good2", 
    [795]   = "good3", 
    [1095]  = "hero" 
} 
  
-- this function will return the reputation name giving him the  reputation points 
-- i.e getReputationName(-120) will return "evil" 
function getReputationName( points ) 
    for reppoints, repname in ipairs ( reputations ) do -- loop over all reputations 
        result = repname 
        --stop the loop if the rep from the table is higher than the rep of the player 
        if reppoints > points then 
            return result --we found it, so let's return it 
        end 
    end 
end 
  
-- this function will update the reputation name of the player 
-- if the player has -120, getElementData on "Reputation.status" will return "evil" 
function updateReputationName( player ) 
    local repPoints = getElementData(killer, "Reputation.points") or 0 
    local newRepName = getReputationName( repPoints ) 
    setElementData(player, "Reputation.status", newRepName) 
end 
  
function reputation(ammo, killer, weapon, bodypart) 
    if killer and killer ~= source then 
        --Reputation points of the guy who died 
        local dRPoints = getElementData(source, "Reputation.points") or 0 
  
        --Reputation points of the guy who killed him 
        local kRPoints = getElementData(killer, "Reputation.points") or 0 
  
        if ( dRPoints < 0 or dRPoints == kRPoints ) then -- the guy who died was a bad guy 
            kRPoints = kRPoints + 5 
        else --was a good guy 
            kRPoints = kRPoints - 5 
        end 
        setElementData(killer, "Reputation.points", kRPoints) 
        updateReputationName( killer ) -- update the reputation name for the killer 
    end 
end 
addEventHandler( "onPlayerWasted", getRootElement(), reputation) 
  
function onLogin (_, account) 
    local defaultPoints = 0 --you can change the default points a guy should have here 
    local defaultRepName = getReputationName( defaultPoints ) --do not modify here 
    setElementData(source, "Reputation.status", getAccountData(account, "rep") or defaultRepName) 
    setElementData(source, "Reputation.points", getAccountData(account, "re.p") or defaultPoints) 
end 
addEventHandler ("onPlayerLogin", root, onLogin) 
  
function saveData(thePlayer, theAccount) 
    if (theAccount and not isGuestAccount(theAccount)) then 
        setAccountData (theAccount, "rep", getElementData(thePlayer, "Reputation.status")) 
        setAccountData (theAccount, "re.p", getElementData(thePlayer, "Reputation.points")) 
    end 
end 
  
addEventHandler ("onPlayerQuit", root, function () saveData(source, getPlayerAccount(source)) end) 
addEventHandler ("onPlayerLogout", root, function (prev) saveData(source, prev) end) 

Link to comment

That's because there is a typo at "updateReputationName" function, instead of "player" it says "killer".

function updateReputationName( player ) 
    local repPoints = getElementData(player, "Reputation.points") or 0 
    local newRepName = getReputationName( repPoints ) 
    setElementData(player, "Reputation.status", newRepName) 
end 

Use that.

Link to comment
  • Moderators

I also rechecked my function getReputationName which was totally wrong.

Run this code and see the result :o

https://ideone.com/zLMTY5

So after you did the Solidsnake's fix, change your code to be like this:

local reputations = { 
    {-1095  , "demon"   }, 
    {-795   , "evil"    }, 
    {-10    , "bad2"    }, 
    {-5     , "bad1"    }, 
    {0      , "normal"  }, 
    {5      , "good1"   }, 
    {10     , "good2"   }, 
    {795    , "good3"   }, 
    {1095   , "hero"    } 
} 
  
function getReputationName( points ) 
    local _, data = next( reputations ) 
    local result = data[2] 
    for k, repdata in ipairs ( reputations ) do 
        if repdata[1] <= points then 
            result = repdata[2] 
        else 
            break 
        end 
    end 
    return result 
end 

Link to comment
I also rechecked my function getReputationName which was totally wrong.

Run this code and see the result :o

https://ideone.com/zLMTY5

So after you did the Solidsnake's fix, change your code to be like this:

local reputations = { 
    {-1095  , "demon"   }, 
    {-795   , "evil"    }, 
    {-10    , "bad2"    }, 
    {-5     , "bad1"    }, 
    {0      , "normal"  }, 
    {5      , "good1"   }, 
    {10     , "good2"   }, 
    {795    , "good3"   }, 
    {1095   , "hero"    } 
} 
  
function getReputationName( points ) 
    local _, data = next( reputations ) 
    local result = data[2] 
    for k, repdata in ipairs ( reputations ) do 
        if repdata[1] <= points then 
            result = repdata[2] 
        else 
            break 
        end 
    end 
    return result 
end 

I kinda thought it was wrong, but since I thought it was already tested I didn't try to change it.

Link to comment
  • Moderators
I kinda thought it was wrong, but since I thought it was already tested I didn't try to change it.

Hehe, I never test them because I too lazy to start a server, but I should test it in ideone, I know :oops:

But this time my function has been tested and is workign fine now:

https://ideone.com/q3wLNF

now when i kill somebody my points are 5 but status is still normal

By the way, now you didn't apply the Solidsnake's fix right ? :wink:

The only reason for that bug is that the following lisne is returning false:

local repPoints = getElementData(player, "Reputation.points") or 0 

So I think you didn't replaced killer by player as Solidsnake told you.

Try this again but with an output like this:

function updateReputationName( player ) 
    local repPoints = getElementData(player, "Reputation.points") or 0 
    local newRepName = getReputationName( repPoints ) 
    outputChatBox( repPoints.." => "..newRepName) 
    setElementData(player, "Reputation.status", newRepName) 
end 

And tell us what's written in the chabox when you kill someone.

I'm also pretty sure you aren't using /debugscript 3 to be able to see client side errors. You won't see them in the server's console.

If it's still not working show us the code you have now since we did a lot of modifications. I want to see if you did them correctly.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...