Jump to content

Vehicle Idle Time


Recommended Posts

It is untested, not sure if this is what you wanted but:

If a player keeps driving a vehicle for a while, it will increment his "driving" element data. If he exits, the element data is set back to nil.

local time = 5 * 60000 --The time between each check. 
local increment = 100 --How much the element data "driving" should be increased 
function countDrivingTime() 
    local thePlayers = getElementsByType("player") --Get all players 
    for key, thePlayer in ipairs(thePlayers) do 
        if isPedInVehicle(thePlayer) then -- If thePlayer is in a vehicle 
            local driving = getElementData(thePlayer, "driving")  
            driving = driving and driving + increment or 0 --If driving is a value, then increment it. 
            setElementData(thePlayer, "driving", driving) --Set the data. 
        end 
    end 
end 
setTimer(countDrivingTime, time, 0) --Timer to check the players. 
  
--Reset driving counter when player exits, remove it if you don't want it. 
addEventHandler("onPlayerVehicleExit", root, 
function() 
    if getElementData(source, "driving") then 
        setElementData(source, "driving", nil) 
    end 
end) 

However, if you want to measure the IDLE TIME ( that is when the player is in a vehicle, but doesn't move with it at all ); You're going to have to save the old position, and then see if it equals the current position in this check.

Link to comment
Dude....this is to check if the player is not moving...

Yes, if he's still then he's just camping.

If you want to make it more exact, just store down the value to a variable for the player and check it after the time you want, and if the distance is okay (getDistanceBetweenPoints2D) then go foward. (But i dont really recommend this)

Link to comment

Okay, I changed it to check distance from old position to current position.

local time = 5 * 60000 --The time between each check. 
local increment = 100 --How much the element data "driving" should be increased 
function countDrivingTime() 
    local thePlayers = getElementsByType("player") --Get all players 
    for key, thePlayer in ipairs(thePlayers) do 
        if isPedInVehicle(thePlayer) then -- If thePlayer is in a vehicle 
            local pos = getElementData(thePlayer, "oldpos") or {0, 0, 0} 
            local oldx, oldy, oldz = unpack(pos) 
            local x, y, z = getElementPosition(thePlayer) 
            if getDistanceBetweenPoints3D(x, y, z, oldx, oldy, oldz) > 5 then --If he moved a distance of 5 away from old position 
                local driving = getElementData(thePlayer, "driving") 
                driving = driving and driving + increment or 0 
                setElementData(thePlayer, "driving", driving) 
            end 
            setElementData(thePlayer, "oldpos", {x, y, z}) 
        end 
    end 
end 
setTimer(countDrivingTime, time, 0) --Timer to check the players. 
  
--Reset driving counter when player exits, remove it if you don't want it. 
addEventHandler("onPlayerVehicleExit", root, 
function() 
    setElementData(source, "driving", nil) 
    setElementData(source, "oldpos", nil) 
end) 

However, if the timer is as long as 5 minutes, then if a player went from point x to point y, then returned to point x in 5 mins; His driving data isn't going to be incremented because the distance from the old position (point x) to the current position ( point x as well) is 0.

Link to comment
Okay, I changed it to check distance from old position to current position.
local time = 5 * 60000 --The time between each check. 
local increment = 100 --How much the element data "driving" should be increased 
function countDrivingTime() 
    local thePlayers = getElementsByType("player") --Get all players 
    for key, thePlayer in ipairs(thePlayers) do 
        if isPedInVehicle(thePlayer) then -- If thePlayer is in a vehicle 
            local pos = getElementData(thePlayer, "oldpos") or {0, 0, 0} 
            local oldx, oldy, oldz = unpack(pos) 
            local x, y, z = getElementPosition(thePlayer) 
            if getDistanceBetweenPoints3D(x, y, z, oldx, oldy, oldz) > 5 then --If he moved a distance of 5 away from old position 
                local driving = getElementData(thePlayer, "driving") 
                driving = driving and driving + increment or 0 
                setElementData(thePlayer, "driving", driving) 
            end 
            setElementData(thePlayer, "oldpos", {x, y, z}) 
        end 
    end 
end 
setTimer(countDrivingTime, time, 0) --Timer to check the players. 
  
--Reset driving counter when player exits, remove it if you don't want it. 
addEventHandler("onPlayerVehicleExit", root, 
function() 
    setElementData(source, "driving", nil) 
    setElementData(source, "oldpos", nil) 
end) 

However, if the timer is as long as 5 minutes, then if a player went from point x to point y, then returned to point x in 5 mins; His driving data isn't going to be incremented because the distance from the old position (point x) to the current position ( point x as well) is 0.

I guess it will add 100 in Driving ElementData only once not every 5 minutes, I didn't tested but it seems that there is something wrong with the timer...

And thanks for your work i really appreciate it.

Link to comment

You should make the checks client side for the local player only, so that you don't need to loop trough every player on the server-side... Because element data is synced with everything on the server...

You can also check if the player is driving with getControlState ( https://wiki.multitheftauto.com/wiki/GetControlState )

So you should recode the code a little bit optimized for client side, just recommed ;p

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