[Release] Timetrials - Standalone Races and Leaderboard [v1.00]

I need help in the same way, if u find some answers pls contact me…

I suppose that the money system of each server is different and must be done by the one who created the server.

This should work:

ESX = exports['es_extended']:getSharedObject()

-- Save score and send chat message when player finishes
RegisterServerEvent('racePlayerFinished')
AddEventHandler('racePlayerFinished', function(source, message, title, newScore)
    -- Get top car score for this race
    local xPlayer = ESX.GetPlayerFromId(source)
    local msgAppend = ""
    local msgSource = source
    local msgColor = color_finish
    local allScores = getScores()
    local raceScores = allScores[title]
    if raceScores ~= nil then
        -- Compare top score and update if new one is faster
        local carName = newScore.car
        local topScore = raceScores[carName]

        xPlayer.addMoney(10000)

        if topScore == nil or newScore.time < topScore.time then
            -- Set new high score
            topScore = newScore

            xPlayer.addMoney(25000)
            
            -- Set message parameters to send to all players for high score
            msgSource = -1
            msgAppend = " (fastest)"
            msgColor = color_highscore
        end
        raceScores[carName] = topScore
    else
        -- No scores for this race, create struct and set new high score
        raceScores = {}
        raceScores[newScore.car] = newScore
        
        -- Set message parameters to send to all players for high score
        msgSource = -1
        msgAppend = " (fastest)"
        msgColor = color_highscore
    end
    
    -- Save and store scores back to file
    allScores[title] = raceScores
    saveScores(allScores)
    
    -- Trigger message to all players
    TriggerClientEvent('chatMessage', -1, "[RACE]", msgColor, message .. msgAppend)
end)

it is not working for me sadly i get this error : SCRIPT ERROR: @timetrials/timetrials_sv.lua:63: attempt to index a nil value (local ‘xPlayer’)

Are you using ESX?

yes i use ESX 1.7.5

Where exactly (and in what file) should I add this code to not get NULL for addon cars?
Citizen.CreateThread(function() AddTextEntry("vehicle spawn/model name", "display name you want") end)

Even tho this is so old, goddam it is still so good. Remember using this back in the day on my server, just used some of the code :pray: for my server.

Hi is there any way i can set for players get money for racing? Thanks

Hello, very good script, I have been using it for a long time and I love it, I wanted to know if there was a way to change the name of the player to the name of the character in the scoreboard ?

what sould i change to work with QB ? :innocent:

Is there any way to get addon cars to show up as not ‘NULL’

I’ve started a fork and posted some updates. I’ll try to help anyone I can over here. TimeTrials Rework

1 Like

Hey, i’m using your script, everything is fine on my local server, but when i put it on my online server (zap-hosting), the scores.txt doesnt create and dont save scores from players. Any idea about how i can solve that ?

You’ll have to move the scores file into the time trials folder and convert it into a json

Hey boss, if you have not fixed the issue with the leaderbords not showing
add local json = require(“json”) to the top of the timetrials_sv.lua

it solved my issue

Has anyone else had the script break since version 3258? It’s randomly corrupting player scores when they finish a race, though it doesn’t happen every time. It worked fine before, just curious if it’s an isolated case or if anyone else is experiencing this…

for anyone having issues with this resource lately (or any that save to a .json tbh) its due to implementation of security sandbox and recent problem with io.open - “w” and “w+” not correctly writing to file

the fixes for now are to make sure your times/scores .json is saving inside your timetrial resource folder to solve the sandbox issue.
I added absolute path for the .json and that was solved

local scoreFileName = "./resources/[scripts]/Timetrials/scorestrial.json"

and for the io.open writing issue I added some functions to completely remove the .json and recreate it with an empty array before writing any new data to it like so.

-- Create a new .json if one doesn't exist 
function initializeScores()
    local file = io.open(scoreFileName, "r")
    if not file then
        local f = io.open(scoreFileName, "w+")
        if f then
            f:write("[]")
            f:close()
        end
    else
        file:close()
    end
end
initializeScores()  -- create .json in the correct folder on resource start if doesn't exist

-- Delete .json file and create new
function clearScores()
    local file = os.remove(scoreFileName) -- delete .json
    
	initializeScores() -- recreate .json after removing
end

then just add clearScores before any new Times/Scores are saved like

	-- Temp Fix
	clearScores()
	
    -- Save and store scores back to file
    allScores[title] = raceScores
    saveScores(allScores)

Or alternatively you can also fix this by using the SaveResourceFile Native, replacing the saveScores Function with something like

function saveScores(scores)
    SaveResourceFile("Timetrials", "scorestrial.json", allScores, -1)
end

this is not how i fixed mine and is basic example but I tested to make sure it writes correctly and it does so should be fine :+1:

This has affected more than just this timetrial resource so if you notice any other resources with problems saving to .json and not sure how to fix or just tired of fixing them you can go to this post and let CFX know so they are more likely to fix :v:

1 Like