Jump to content

This part of script have some error?


Mario222

Recommended Posts

The resource called race_scoringsystem https://community.multitheftauto.com/index.php?p=resources&s=details&id=1073 have some error in s_main because in console says

ERROR: race_scoringsystem\s_main.lua:86 attempt to perform arithmetic on fiel 'ratio_opponentrank' (a nil value). And resource doesn't work. What happend here?

 

 

 

PlayerData = {}

function ResetPlayerData(player)
    local playerName = getPlayerName(player or source)
    PlayerData[playerName] = { 
        Streaks = 0
    }
end
addEventHandler("onPlayerJoin", root, ResetPlayerData)


addEventHandler("onResourceStart", resourceRoot, 
    function()
        for _,player in ipairs(getElementsByType("player")) do
            ResetPlayerData(player)
        end
    end
)

addEvent("onPlayerRaceWinStreak")
addEvent("onPlayerAwardPoints")
addEvent("onPlayerStatsUpdate")
addEventHandler("onPlayerStatsUpdate", root, 
    function(playerName, key, value, oldValue)
        value = value or 0
        oldvalue = oldvalue or 0
        local delta = value - oldValue
        if delta == 0 then
            return
        end
        
        local points = g_Settings[g_AutoPointSettings[key]]
        if points and points ~= 0 then
            exports["race_playerstats"]:IncrementPlayerStat(source, STAT.POINTS, points)
            if g_SettingLabels[key] then
                triggerEvent("onPlayerAwardPoints", source, points, g_SettingLabels[key])
            end
        end
        
        -- the following require players
        local players = getElementsByType("player")
        
        if key == STAT.TIMES_DIED then
            local pointsDeath = g_Settings["ratio_death"]
            exports["race_playerstats"]:IncrementPlayerStat(source, STAT.POINTS, pointsDeath)
            triggerEvent("onPlayerAwardPoints", source, pointsDeath, g_SettingLabels[STAT.TIMES_DIED])
            local pointsOpponentDeath = g_Settings["ratio_opponentdeath"]
            if pointsOpponentDeath == 0 then return end
            for _,player in ipairs(players) do
                if player ~= source then
                    exports["race_playerstats"]:IncrementPlayerStat(player, STAT.POINTS, pointsOpponentDeath)
                    triggerEvent("onPlayerAwardPoints", player, pointsOpponentDeath, string.format("%s wasted!", playerName))
                end
            end
            return
        end
        
        -- count opponent ranks and points
        if key == STAT.RACES_FINISHED then
            local playerRank = getElementData(source, "race rank", false)
            --local playerPoints = exports["race_playerstats"]:GetPlayerStatValue(source, STAT.POINTS) or 0
            local pointsFinish = g_Settings["ratio_racefinish"]
            exports["race_playerstats"]:IncrementPlayerStat(source, STAT.POINTS, pointsFinish)
            triggerEvent("onPlayerAwardPoints", source, pointsFinish, g_SettingLabels[STAT.RACES_FINISHED])

            if g_Settings["ratio_opponentrank"] == 0 and g_Settings["ratio_opponentpoints"] == 0 then return end
            
            local myPoints = exports["race_playerstats"]:GetPlayerStatValue(source, STAT.POINTS) or 0
            local ranks = 0
            local points = 0
            local oppPoints = 0
            for _,player in ipairs(players) do
                -- opponents
                if player ~= source then
                    local opponentRank = getElementData(player, "race rank", false) or 999
                    if opponentRank > playerRank then
                        ranks = ranks + 1
                        local opponentPoints = exports["race_playerstats"]:GetPlayerStatValue(player, STAT.POINTS) or 0
                        local weight = myPoints / (myPoints + opponentPoints)
                        if weight > 1 then weight = 1 end
                        oppPoints = oppPoints + (1-weight) * g_Settings["ratio_opponentpoints"]
                    end
                end
            end
            local rankPoints = ranks * g_Settings["ratio_opponentrank"]
            if rankPoints >= 1 then
                exports["race_playerstats"]:IncrementPlayerStat(source, STAT.POINTS, rankPoints)
                triggerEvent("onPlayerAwardPoints", source, rankPoints, getRankText(playerRank))
            end
            if oppPoints >= 1 then
                exports["race_playerstats"]:IncrementPlayerStat(source, STAT.POINTS, oppPoints)
                triggerEvent("onPlayerAwardPoints", source, oppPoints, "Beaten Racers")
            end
            return
        end
        
        -- count streaks
        if key == STAT.RACES_WON then
        
            local playerName = getPlayerName(source)
            if not PlayerData[playerName] then
                ResetPlayerData(source)
            end

            -- but only if there is a quorum
            local minPlayers = g_Settings["winning_streak_quorum"] or 0
            if #players < minPlayers then 
                PlayerData[playerName].Streaks = 0
                return 
            end

            local wins = PlayerData[playerName].Streaks or 0
            wins = wins + 1
            
            if wins > 1 then
                exports["race_playerstats"]:IncrementPlayerStat(source, STAT.POINTS, g_Settings["ratio_winstreak"])
                triggerEvent("onPlayerRaceWinStreak", source, wins)
                triggerEvent("onPlayerAwardPoints", source, g_Settings["ratio_winstreak"], string.format("Winning Streak x%u!", wins))
            end
            
            local previousStreaks = exports["race_playerstats"]:GetPlayerStatValue(source, STAT.MAX_STREAKS) or 0
            if wins > previousStreaks then
                exports["race_playerstats"]:IncrementPlayerStat(source, STAT.MAX_STREAKS, 1)
            end

            PlayerData[playerName].Streaks = wins
            
            -- clear opponents streak
            for _,player in ipairs(players) do
                if player ~= source then
                    local oppName = getPlayerName(player)
                    if PlayerData[oppName] then
                        PlayerData[oppName].Streaks = 0
                    end
                end
            end
            
            return
        end

    end
)

Link to comment

Hello Mario222,

can you open up your admin panel and see whether the race_scoringsystem resource does have a "race_opponentrank" setting assigned to it? If the setting is missing then this error could be happening. Please post a screenshot of the available settings of said resource.

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...