Help with a revive script

Hi guys,

I really need some help with this script I have been working on for the past few days. I believe that the reason it isn’t working is that I am not able to get the proper xPlayer (aka the player who ran the /previve command), in order to run the rest of the command. Can anyone help me with this? I think it’s a small fix. Basic idea of the script is to check that there are 0 EMS on, and if there isn’t any EMS on it just checks that the person is next to the player, and then it revives them (along with doing the cpr emote).

Here is the script:

ESX=nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

ESX.RegisterCommand('previve','user', function(source)  
    local _source = source
    local xPlayer = ESX.GetPlayerFromId(_source)
    if xPlayer~=nil then
    local plyCoords1 = xPlayer.getCoords(true)
    local closestPlayer,closestDistance=ESX.Game.GetClosestPlayer(plyCoords1)    
    
    if EmsCount==0 and closestDistance<10 and closestPlayer~=-1 then    
        TaskStartScenarioAtPosition(xPlayer,'CODE_HUMAN_MEDIC_TEND_TO_DEAD',plyCoords1.x,plyCoords1.y,plyCoords1.z,GetEntityHeading(PlayerPedId()),0,0,false)
        Citizen.Wait(7000)
        closestPlayer.triggerEvent('esx_ambulancejob:revive')
        ClearPedTasks(ply) 
    
    end
end 
end)


Citizen.CreateThread(function()
    EmsCount=0
    local xPlayer = ESX.GetExtendedPlayers('job', 'ambulance') -- Returns xPlayers with the ambulance job
 	for _, xPlayer in pairs(xPlayer) do
		EmsCount=EmsCount+1
	end
    return EmsCount
end)

Thanks a lot guys!!!

Hey! the issue is that you have a mix of client only and server only functions here :slight_smile:

As you can see here as well, ESX.RegisterCommand already returns the xPlayer table, not source :slight_smile:

This Example with work on ESX 1.6 + :slight_smile:

ESX= exports["es_extended"]:getSharedObject() -- export is better than the event

ESX.RegisterCommand('previve','user', function(xPlayer) -- returns the xPlayer table of the executing user
    local plyCoords1 = xPlayer.getCoords(true)
    local closestPlayer = ESX.OneSync.GetClosestPlayer(plyCoords1, 10.0, {[xPlayer.source] = true) -- new functions with no documenation yet, but its server sided checks :)
    local EmsCount = ESX.Table.SizeOf(ESX.GetExtendedPlayers('job', 'ambulance')) -- Gets all ambulance players 
    if EmsCount == 0 and closestPlayer then -- make sure there is a closeby player
       Wait(10000) -- Note: Scenario Natives dont seem to be usable from the server
        TriggerClientEvent('esx_ambulancejob:revive', closestPlayer.id)
    end
end 
end)

Mycroft,
Director Of ESX-Framework :heart:

1 Like

For some reason, there is still an error:

[  script:es_extended] SCRIPT ERROR: citizen:/scripting/lua/scheduler.lua:485: attempt to concatenate a nil value (local 'err')
[      script:previve] SCRIPT ERROR: @previve/server/previve-s.lua:5: attempt to index a nil value (field 'OneSync')
[      script:previve] > ref (@es_extended/server/functions.lua:130)

Would really appreciate any and all help :frowning:

Thank you

I figured out the reason why it isn’t working. You said it only worked for version 1.6+, we’re running Legacy ESX 1.4.2. What sort of implementation would work on that version?

Best,
Whiffed

Since your on legacy already, updating ESX is as simple as just replacing the es_extended files :slight_smile: , no other work needed and it gets you loads better performance and more functions :smile:

Mycroft,
Director Of ESX-Framework :heart:

1 Like

Can you send me a link to the latest ESX & tell me exactly which files I would need to replace?

Really appreciate your help! You’re amazing :heart:

Thanks!

you can find the latest ESX here :slight_smile:

I do recommend updating all default files since they have all had huge rewrites recently, dropping the overall resmon of a server running base ESX + CFX default resources down to 0.14ms for the overall server :slight_smile:

But, the easiest and most simple way to update is to grab the es_extended folder from your the [esx] folder and replace the one in your server, all legacy versions are 100% compatible with eachother so you wont have to worry about any scripts breaking :slight_smile:

And, no problem :smile:
Mycroft,
Director Of ESX-Framework :heart:

1 Like

Alright, I upgraded the ESX version and now I am getting this error.

[      script:previve] SCRIPT ERROR: Execution of native 000000002f7a49e6 in script host failed: Argument at index 1 was null.
[      script:previve] > ref (@es_extended/server/functions.lua:130)
[  script:es_extended] SCRIPT ERROR: citizen:/scripting/lua/scheduler.lua:485: attempt to concatenate a nil value (local 'err')

I’m not sure how to fix this… Any ideas?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.