/heal command, your first script
By the end of this lesson you will type /heal in chat and watch your character refill to full health, with this command's cooldown enforced on the server.
Build it
Make the resource folder
Inside your server's resources folder, create this folder:
resources/qu_heal_command
Create the files
Create this exact file layout:
resources/qu_heal_command/
fxmanifest.lua
server.lua
client.lua
Write fxmanifest.lua
Open fxmanifest.lua and paste this:
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'
server_script 'server.lua'
Write the lesson code
Open server.lua and paste this:
local cooldown = {}
RegisterCommand('heal', function(src)
local now = os.time()
if cooldown[src] and now - cooldown[src] < 60 then
TriggerClientEvent('qu_heal_command:deny', src, 60 - (now - cooldown[src]))
return
end
cooldown[src] = now
TriggerClientEvent('qu_heal_command:heal', src)
print('[qu_heal_command] approved heal for ' .. GetPlayerName(src))
end, false)
Open client.lua and paste this:
RegisterNetEvent('qu_heal_command:heal', function()
local ped = PlayerPedId()
SetEntityHealth(ped, GetEntityMaxHealth(ped))
print('[qu_heal_command] healed')
end)
RegisterNetEvent('qu_heal_command:deny', function(seconds)
print('[qu_heal_command] wait ' .. seconds .. ' seconds')
end)
Start and test it
Open server.cfg and add this line:
ensure qu_heal_command
Save. Now start the resource in the live console (txAdmin Live Console or the FXServer console window) by typing:
ensure qu_heal_command
Use ensure, not restart, the first time: the line you just added to server.cfg is only read when the whole server boots, so on an already-running server the resource has not started yet. restart only works on a resource that is already running, so it would do nothing here. ensure starts the resource if it is stopped and restarts it if it is already running, so it always works.
Join your own server, wait until your character has spawned in, then open the in-game chat box (press the chat key, by default T) and type the command, then press Enter:
/heal
Keep reading the full lesson
Sign in to start, then unlock every step of this lesson and the full FiveM School with a membership.
- How it works
- Now revive someone else
- If something went wrong
- What you can do now
- Try it yourself
The remainder of /heal command: your first build is available to FiveM School members.