Jump to content

Infinite timer inside onVehicleEnter


Jose76Tron

Recommended Posts

So, i have a problem, im making a script for replacing sounds of cars, the first car i enter works nice, when i change engine state the sound goes off and on but when left a car on with the replaced sound and try to enter to another car and start the engine, it plays the sound on the last car, so if you do this with more cars, the first car plays a lot of sounds. Sorry my english is a bit :~ty cause im spanish.

 

Heres  some lua code, server side

local wayfarerenginestate = 0
local timerwayfarersound = {}

function OnEnterVehicle ( thePlayer, seat, jacked )
    if getElementModel( source ) == 586 then
        local texture2
        local sourcetxt = tostring(source)
        if string.match(sourcetxt, " ") then
            local extracted = sourcetxt:match("% .+")
            texture2 = extracted:gsub("% ","") 
        end
        outputChatBox( texture2 )
        if getElementData( source, "timer") == false then
            outputChatBox ( "Timer started", thePlayer )
            timerwayfarersound[texture2] = setTimer( checkwayfarerenginestate, 500, 0, source, texture2 )
            setElementData( source, "timer", true)
        end
    end
end
addEventHandler ( "onVehicleEnter", getRootElement(), OnEnterVehicle )

function checkwayfarerenginestate (source, texture2)
    if getVehicleEngineState( source ) == true and wayfarerenginestate == 0 then
        triggerClientEvent( "wayfarersoundcevent", resourceRoot, source, texture2)
        wayfarerenginestate = 1
    elseif getVehicleEngineState( source ) == false and wayfarerenginestate == 1 then
        triggerClientEvent( "wayfarersounddcevent", resourceRoot, texture2)
        wayfarerenginestate = 0
    end
end

 

Client side:

local wayfarerenginestate = 0
local wayfarersound = {}

function wayfarersoundf ( veh, texture2 )
    local x, y, z = getElementPosition( veh )
    wayfarersound[texture2] = playSound3D( "sounds/wayfarersound.mp3", x, y, z, true )
    setSoundVolume( wayfarersound[texture2], 0.45 )
    setSoundMaxDistance( wayfarersound[texture2], 100 )
    attachElements( wayfarersound[texture2], veh)
end
addEvent( "wayfarersoundcevent", true )
addEventHandler( "wayfarersoundcevent", resourceRoot, wayfarersoundf )

function wayfarersounddf (texture2)
    destroyElement( wayfarersound[texture2] )
end
addEvent( "wayfarersounddcevent", true )
addEventHandler( "wayfarersounddcevent", resourceRoot, wayfarersounddf )

What is the problem? Which 1 vehicle it works nice but, which more than 1 it bugs

Link to comment
15 hours ago, Jose76Tron said:

Snip

I fixed everything just adding another table for the check

Server

local wayfarerenginestate = {}
local wayfarerenginestate = {}

function checkwayfarerenginestate (vehicle, vehicleid)
    if getVehicleEngineState( vehicle ) == true and wayfarerenginestate[vehicleid] == 0 then
        triggerClientEvent( "wayfarersoundcevent", resourceRoot, vehicle, vehicleid)
        wayfarerenginestate[vehicleid] = 1
    elseif getVehicleEngineState( vehicle ) == false and wayfarerenginestate[vehicleid] == 1 then
        triggerClientEvent( "wayfarersounddcevent", resourceRoot, vehicleid)
        wayfarerenginestate[vehicleid] = 0
    end
end
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...