[RELEASE] [ESX] Defib (Updated 18/10/2020)

I loved to do it!
<3

1 Like

I saw and followed the new README but it is still the same symptom.

@dlrbgh0313 Can you show me how your server.lua from ambulancejob looks like please ?

main.lua from esx_ambulancejob/server shared in Description.

TYSM. Finally it works :slight_smile:

2 Likes

I have everything needed, changed medkit label to Defib but when I go to revive someone with K all I get is that I donā€™t have a defib in my inventory though I do.

BE carreful it has to be a ā€œmedikitā€ not a ā€œmedkitā€ . You have the correct item value in the client.lua at the first line. If you are completely stuck, drop me your discord in MP and I will help you.

I totally forgot that its setup as medikit instead of medkit, will test again, thanks!

1 Like

Same here. We tried this on our server, everything seemed to work including the animation, but the revive does not occur.

Is there a way to make this work without using a keybind but rather just simply ā€œUseā€ through the inventory menu?

we done this

Blockquote

Even I try to replace your

Blockquote

Still Players Not Getting Revive !! Need a fix ! else good script if we can use it :slight_smile:

@Hypnotic_92 There is probably a way to modify this but havenā€™t tested it yet.
@subhampro There is no fix needed for this script as we were able to have this working for other players/users.

Do you guys have kept in mind all the requirements and also make sure to use the item from esx_ambulancejob, which is ā€œmedikitā€ ?

Also do not rename the script folder ā€œdefibā€ to something else it has to stay as it is.

If you need assistance PM me so we can have a look with screen sharing.

Cheers

i want of vrp.

Iā€™ve never been on VRP tho, canā€™t really help you on that side mate

This is really heavy for server it may cause hitch time warning care.

Script doesnā€™t seem to work, keep getting this error in console.

Error parsing script @defib/server.lua in resource defib: @defib/server.lua:23: ā€˜endā€™ expected (to close ā€˜functionā€™ at line 6) near
Failed to load script server.lua.

How i can change SCENARIO to anim ?
Because i try to do something like this but it doesnt work

client.lua (1.7 KB)

hey so i tried getting it everything works the animation but it uses like 3 medikits and wont even revive the player y is that

Hi everyone,

Iā€™ve taken some time to Update this script with the current manifest version of FiveM.
Iā€™ve also translated all in game notifications from french to english for this script.

All files within my github are updated since the 18th of August 2020.

As I am not a server owner anymore on FiveM, I would be glad to help someone who needs this script to set it up on his server. Feel free to contact me so we can have a look remotely.

Since the changes that have been made on FiveM are not drastic, I am assuming that the script should be working smoothly with the current update that I made.

@ORB_DEV @GrimVisuals

the ā€˜Kā€™ How can I keybinding to Chat Command
Citizen.Wait(10)

if(IsControlJustReleased(1, 311))then

  TriggerServerEvent('defib:getAmbulancesCount')

end

end

end)
i want to chang toRegisterCommand(ā€˜healthā€™, function(source, args, rawCommand)

Weā€™ve been wondering why our shiny new RP server has been dying and encountering server hitches with increasing frequency after a few hours and unusable in 12 - 14. We ran some profiling on the server with the built in tools and found the defib had some large numbers against it, although a specific line number wasnā€™t listed as the culprit. After inspection it became clear what is wrong. One thing that is seriously bugging about modding for online game communities is that itā€™s fairly easy to do and LUA in particular whilst obscure, is a pretty decent language and people can get cracking pretty fast. The biggest problem is it isnā€™t type safe as with compiled language and IDE support limited so you donā€™t get hints as to where you may have done things poorly. Also, just jumping in and learning coding through this sounds great, but when you cause people weeks of agony from tacking on another published resource which is substandard onto the mass of crap out there the burden on the consumers to fix what should be clean to start with is ridiculous. I donā€™t expect everything to be perfect but please be careful when you decide to make something public, do testing, testing and more testing, learn about coding, donā€™t just throw code out there. Frankly, the issue Iā€™ve spotted here is inexcusable. Soā€¦ when you actually LEARN coding properly youā€™ll reach the topic of recursive algorithms which can be used to create a small unit that can be reused and run over and over seeding each call with the next piece of information to process. It is absolutely critical that you provide a guard clause with a condition to return which ensures that your recursion doesnā€™t keep nested new calls forever, the key reason being that a call to a function allocates memory to hold the variables required for the function instance to execute. The memory is put onto the stack. A stack is a first in last out concept and as you add more allocations onto the stack you have more memory to unwind (IF you return and unwind). The only process that deallocates the memory is the function ending and returning to the caller. In the case of recursion you are dumping a new instance of the functionā€™s initialised memory block onto the stack over and over again eventually filling up all available RAM.

So in regards to this defib script, note that there is a SetTimeout function that waits for 5 seconds and calls a function, which is the same function weā€™re in. Where is the guard clause? This recursive call goes on forever and if the 5000ms pause wasnā€™t there, your server would be dead in minutes, probably less. Memory is being allocated on each call and never released. Here is the code as currently published:

local ESX = nil
local ambulancesConnected = 0

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

function CountAmbulances()
	local xPlayers = ESX.GetPlayers()
	ambulancesConnected = 0
	for i=1, #xPlayers, 1 do
		local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
		if xPlayer.job.name == 'ambulance' then
			ambulancesConnected = ambulancesConnected + 1
		end
	end
	SetTimeout(5000, CountAmbulances)
end
CountAmbulances()

RegisterServerEvent('defib:getAmbulancesCount')
AddEventHandler('defib:getAmbulancesCount', function()
  TriggerClientEvent('defib:useDefib', source, ambulancesConnected)
end)

When I first looked at it I didnā€™t see any Citizen threads running which was a candidate for killing performance and it took awhile to realise the SetTimeout call was the likely candidate. You need to ask yourself, why does this script need to run that function every 5 seconds? Especially, why recursively? What it does is re-calculate the number of ambulances online, which only needs to be known when the debib is used. This script relies on ESX, so rather than loop and calculate here you could increase the ambulance count in an esx event handler for onPlayerConnected and decrement with onPlayerDisconnected. No loops just event handler subscribed to the ESX backend. In a pinch I decided to go with removing the initial global scoped call to CountAmbulances() and retain the method, but call it only when a defib is actually used. It still goes through the process of getting the players and testing for whether theyā€™re an EMS officer, but being all in memory itā€™s not an expensive call. I may go with the event handlers eventually, but for now here is one reasonable way to go which Iā€™m monitoring over the next 12 hours.

local ESX = nil

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

function CountAmbulances()
	local xPlayers = ESX.GetPlayers()
	local ambulancesConnected = 0
	for i=1, #xPlayers, 1 do
		local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
		if xPlayer.job.name == 'ambulance' then
			ambulancesConnected = ambulancesConnected + 1
		end
	end
	
	return ambulancesConnected
end

function UseDefib(source)
	local ambulancesConnected = CountAmbulances()
	TriggerClientEvent('defib:useDefib', source, ambulancesConnected)
end

ESX.RegisterUsableItem('defib', function(source)
	UseDefib(source)
end)

Please check your systems and update if you have the broken recursive approach. There is simply no reason for that insanity.

Once Iā€™ve test this and the event handler approach I will fork and pull request back to the original posterā€™s Github repository. I wanted to share this in advance though to help anyone out who is using this and suffering the pain weā€™ve been going through.

== Update
8 Hours after making the change and not a single server thread hitch.