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
Since you’re calling this one only at the ‘initial declaration’
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
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
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!