Need to restart resource?

Hello!

I recently made a very simple speedometer resource using Lua. It seems to work quite well, but I need to restart the resource in the console after starting my server in order to get it working. Here’s my code if needed:

main.lua:

local ped = GetPlayerPed(-1)
local inSameVehicle = false

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(2)
        if IsPedInAnyVehicle(ped, false) then
            local speed = GetEntitySpeed(GetVehiclePedIsIn(ped, false))*2.2369
            text(math.floor(speed) .. " MPH", 0.17, 0.79)
        end
    end
end)

function text(content, x, y)
    SetTextFont(2)
    SetTextProportional(0)
    SetTextScale(1.4, 1.4)
    SetTextEntry("STRING")
    AddTextComponentString(content)
    DrawText(x, y)
end

__resource.lua:

client_scripts {
    'main.lua'
} 

Any help? Thanks! :slight_smile:

Since you’re calling this one only at the ‘initial declaration’ :slight_smile:

If you only run it at the top ‘initial declaration’ it won’t be set or set properly,
Since it (might have) / has not ‘created your ped’ yet, since that initial declaration happened during you joining and thus 'not being a player yet"

If you update that one in for example a 500ms delayed thread / loop it would be fine :slight_smile:

Edit: YES you could do that in the same thread to, but i like to keep ‘native calls’ as much as possible from ‘high speed threads’ to keep the ‘script resource load’ lower and running at a higher speed :slight_smile:

Edited to be a bit more clear haha

3 Likes

Saved me once again. Thank you so much!

1 Like

Oh crap haha didn’t even noticed you where the “Car Crash Dude” haha until you’ve said “Once again”.

No problem man, glad to help and even more so to see more and more people trying to make their own scripts from scratch while learning from it :slight_smile:

I KNOW mine isn’t the “best guide/directions” out there, but i think it might be of some use to you:

It has some pointers to basic information like objects and also some basic server and client tips/commands for starting devs on lua/FiveM :slight_smile:

Thanks! :wink:

Im having a simalar issue with the doorlocks im using. they work great but only if i restart the resource after ive loaded in. if i dont restart the resource then a couple of the door locks wont register. once restarted they work perfectly until another server restart.

Gotta say man! This helped me phenominally! I was using a global player variable outside of my thread - Im pretty sure moving it into my thread will allow it to capture when Im actually actively playing! GG man!