Jump to content

Another Problem for this script


gytisan

Recommended Posts

addEvent( 'onPlayerRaceWasted',true ) 
addEvent( "stats", true ) 
  
exports[ 'scoreboard' ]:addScoreboardColumn( 'Wins' ) 
exports[ 'scoreboard' ]:addScoreboardColumn( 'Fails' ) 
exports[ 'scoreboard' ]:addScoreboardColumn( 'Skill' ) 
  
local restriction = { } 
  
function restrictionEnd( playerName ) 
    restriction[ playerName ] = nil 
end 
  
function updateRatio ( thePlayer ) 
    if isElement( thePlayer ) then 
        local account = getPlayerAccount( theplayer ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Wins" ) or 0     
            local loses = getAccountData( account, "Fails" ) or 0 
            if loses == 0 then 
                setAccountData( account, "ratio", "-" ) 
                setElementData( thePlayer,'ratio', '-' ) 
            else 
                local kdr = round( wins / loses, 2 ) 
                setAccountData( account, "ratio", kdr ) 
                setElementData( thePlayer,'ratio', kdr ) 
            end 
        end 
    end 
end 
  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Wins" ) or 0 
            local loses = getAccountData( account, "Fails" ) or 0 
            local ratio = getAccountData( account, "Skill" ) or 0 
            if loses and wins then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
  
function getPlayerFromNamePart( thePlayerName ) 
    local thePlayer = getPlayerFromName( thePlayerName ) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs( getElementsByType 'player' ) do 
        if string.find( string.gsub( getPlayerName( thePlayer ):lower( ),"#%x%x%x%x%x%x", "" ), thePlayerName:lower( ), 1, true ) then 
            return thePlayer 
        end 
    end 
    return false 
end 
  
function DestructionWin( vehicle ) 
    local alivePlayers = getAlivePlayers( ) 
    if #alivePlayers == 1 then 
        if isElement( alivePlayers[1] ) then 
            local account = getPlayerAccount( alivePlayers[1] ) 
            if isGuestAccount( account ) then return end 
            local count = tonumber( getAccountData( account,"Wins" ) or 0 ) + 1 
            setAccountData( account,"Wins",count ) 
            setElementData( alivePlayers[1],'Wins', count ) 
            outputChatBox ( "#FFA824The player " .. getPlayerName( alivePlayers[1] ) .. " won!", root, 255, 255, 255, true ) 
        end 
    else 
        local account = getPlayerAccount( source ) 
        if isGuestAccount( account ) then return end 
        local loses = tonumber( getAccountData( account,"Fails" ) or 0 ) + 1 
        setAccountData( account,"Fails", loses ) 
        setElementData( source,'Fails', loses ) 
    end 
end 
  
addCommandHandler("stats", 
    function ( thePlayer, cmd, whoToCheck ) 
        local playerName = getPlayerName( thePlayer ) 
        if restriction[ playerName ] then 
            outputChatBox( "#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true ) 
        else 
            restriction[ playerName ] = true 
            setTimer( restrictionEnd, 30000, 1, playerName ) 
            local player = getPlayerFromNamePart( whoToCheck ) 
            if player then 
                local account = getPlayerAccount( player ) 
                if account then 
                    if isGuestAccount( account ) then return end 
                    local wins = getAccountData( account, "Wins" ) or 0 
                    local loses = getAccountData( account, "Fails" ) or 0 
                    local ratio = math.max( 0, math.min( 100, math.floor( wins / ( loses + wins ) * 100 ) ) ) 
                    outputChatBox( "#FFA824*[sTATS] Player's "..tostring( getPlayerName( player ) ).." stats : " , root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Wins: "..tostring( wins ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Loses: "..tostring( loses ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Ratio: "..tostring( ratio ).."% !", root, 255, 12, 15, true ) 
                end 
            else 
                outputChatBox( "Player not found!" ) 
            end 
        end 
    end 
) 
  
addEventHandler( 'onPlayerJoin',root, 
    function( ) 
        setElementData( source,'Wins', 0 ) 
        setElementData( source,'Fails', 0 ) 
        setElementData( source,'Skill', 0 ) 
    end 
) 
  
addEventHandler( "stats", root, publicstatsinfo ) 
addEventHandler( "onPlayerChat", root, checkCommand ) 
addEventHandler( "onPlayerRaceWasted", root,DestructionWin ) 

This script counts Wins,Fails,Skill-Ratio

The script works very good

But error there. Like: player have 6 wins

player ReLog then join so server again and all stats wins 0 fail 0 skill 0

How to make to script save stats when player relog or exit server :(

Link to comment
addEvent( 'onPlayerRaceWasted',true ) 
addEvent( "stats", true ) 
  
exports[ 'scoreboard' ]:addScoreboardColumn( 'Wins' ) 
exports[ 'scoreboard' ]:addScoreboardColumn( 'Fails' ) 
exports[ 'scoreboard' ]:addScoreboardColumn( 'Skill' ) 
  
local restriction = { } 
  
function restrictionEnd( playerName ) 
    restriction[ playerName ] = nil 
end 
  
function updateRatio ( thePlayer ) 
    if isElement( thePlayer ) then 
        local account = getPlayerAccount( theplayer ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local wins = getAccountData( account, "Wins" ) or 0     
            local loses = getAccountData( account, "Fails" ) or 0 
            if loses == 0 then 
                setAccountData( account, "ratio", "-" ) 
                setElementData( thePlayer,'ratio', '-' ) 
            else 
                local kdr = round( wins / loses, 2 ) 
                setAccountData( account, "ratio", kdr ) 
                setElementData( thePlayer,'ratio', kdr ) 
            end 
        end 
    end 
end  
  
function publicstatsinfo( ) 
    if isElement( source ) then 
        local account = getPlayerAccount( source ) 
        if account then 
            if isGuestAccount( account ) then return end 
            local askdname = getPlayerName( source ) 
            local wins = getAccountData( account, "Wins" ) or 0 
            local loses = getAccountData( account, "Fails" ) or 0 
            local ratio = getAccountData( account, "Skill" ) or 0 
            if (loses and wins) then 
                outputChatBox("#FFA824*" .. askdname .. " won " .. tostring( wins ) .. " times and he lost " ..tostring( loses ).. " times, his ratio is " .. tostring( ratio ) .. "%", root, 255, 12, 15, true) 
            else 
                 outputChatBox("#FFA824*" .. askdname .. " doesen't losed or won " , root, 255, 12, 15, true) 
            end 
        end 
    else 
        outputChatBox( "Error source is not player" ) 
    end 
end 
  
function checkCommand( message, messageType ) 
    if messageType == 0 then 
        if message == "!stats" then 
            triggerEvent( "stats", source ) 
        elseif message == "!st" then 
            triggerEvent( "stats", source ) 
            cancelEvent( ) 
        end 
    end 
end 
  
function getPlayerFromNamePart( thePlayerName ) 
    local thePlayer = getPlayerFromName( thePlayerName ) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs( getElementsByType 'player' ) do 
        if string.find( string.gsub( getPlayerName( thePlayer ):lower( ),"#%x%x%x%x%x%x", "" ), thePlayerName:lower( ), 1, true ) then 
            return thePlayer 
        end 
    end 
    return false 
end 
  
function DestructionWin( vehicle ) 
    local alivePlayers = getAlivePlayers( ) 
    if (#alivePlayers == 1) then 
        if isElement( alivePlayers[1] ) then 
            local account = getPlayerAccount( alivePlayers[1] ) 
            if isGuestAccount( account ) then return end 
            local count = tonumber( getAccountData( account,"Wins" ) or 0 ) + 1 
            setAccountData( account,"Wins",count ) 
            setElementData( alivePlayers[1],'Wins', count ) 
            outputChatBox ( "#FFA824The player " .. getPlayerName( alivePlayers[1] ) .. " won!", root, 255, 255, 255, true ) 
        end 
    else 
        local account = getPlayerAccount( source ) 
        if isGuestAccount( account ) then return end 
        local loses = tonumber( getAccountData( account,"Fails" ) or 0 ) + 1 
        setAccountData( account,"Fails", loses ) 
        setElementData( source,'Fails', loses ) 
    end 
end 
  
addCommandHandler("stats", 
    function ( thePlayer, cmd, whoToCheck ) 
        local playerName = getPlayerName( thePlayer ) 
        if restriction[ playerName ] then 
            outputChatBox( "#FFA824You have to wait 30 seconds", thePlayer, 255, 0, 0, true ) 
        else 
            restriction[ playerName ] = true 
            setTimer( restrictionEnd, 30000, 1, playerName ) 
            local player = getPlayerFromNamePart( whoToCheck ) 
            if player then 
                local account = getPlayerAccount( player ) 
                if account then 
                    if isGuestAccount( account ) then return end 
                    local wins = getAccountData( account, "Wins" ) or 0 
                    local loses = getAccountData( account, "Fails" ) or 0 
                    local ratio = math.max( 0, math.min( 100, math.floor( wins / ( loses + wins ) * 100 ) ) ) 
                    outputChatBox( "#FFA824*[sTATS] Player's "..tostring( getPlayerName( player ) ).." stats : " , root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Wins: "..tostring( wins ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Loses: "..tostring( loses ).."!", root, 255, 12, 15, true ) 
                    outputChatBox( "#FFA824*Ratio: "..tostring( ratio ).."% !", root, 255, 12, 15, true ) 
                end 
            else 
                outputChatBox( "Player not found!" ) 
            end 
        end 
    end 
) 
  
addEventHandler( 'onPlayerJoin',root, 
    function( ) 
        setElementData( source,'Wins', 0 ) 
        setElementData( source,'Fails', 0 ) 
        setElementData( source,'Skill', 0 ) 
    end 
) 
  
addEventHandler( "stats", root, publicstatsinfo ) 
addEventHandler( "onPlayerChat", root, checkCommand ) 
addEventHandler( "onPlayerRaceWasted", root,DestructionWin ) 
  
addEventHandler("onPlayerLogin",root, 
function (_,account) 
    local wins = getAccountData( account, "Wins" ) or 0     
    local loses = getAccountData( account, "Fails" ) or 0 
    if (wins and loses) then 
        setElementData(source,"Wins",wins) 
        setElementData(source,"Fails",loses) 
        updateRatio(source) 
    end      
end) 

Should load the stats on login.

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