[How-To] Create a basic script

Hello everyone, this tutorial will show you how to create a script which will get detected by the server and run successfully. For this tutorial we will create a script which prints a message in the chat each time the player (re-)spawns.


First off, go to the /FiveReborn/resources directory. You should see a lot of folders. Create a new one called “Welcome” and then go in it.

Now server needs to be told that this is a resource. We tell the server to load this by creating a new file in it, called __resource.lua (2x _ !). Now the server understands that this is something that can be loaded.

"That’s it?" you might ask. No, we need to tell the server what to do with this resource and fill it with stuff that should be done afterwards. Lets open the file.

You might see that there’s absolutely nothing in it. Lets begin to fill it with something.

First off, we add a

resource_manifest_version '77731fab-63ca-442c-a67b-abc70f28dfa5'

at the top. This is required since a recent update to access the new goodies that came with it. Basically it tells the server “This was made for the … update”.

Next we add a

client_script "client.lua"

The server will now load this file, which we will create and fill with some code soon. There’s also a server_script "file.lua" if you have ever looked at another script. There’s a difference between a client_script and a server_script. We will only need a client_script for now.

Save the file and create a new file, called “client.lua”. Open it and copy & paste this in it

AddEventHandler("playerSpawned", function(spawn)
    TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Hello world!")
end)

Now lets take a look at what all this means.

AddEventHandler(

This tells the server "I want to do stuff when something specific happens."

"playerSpawned",

"I want to listen for this event."

function(spawn)

"Do the following stuff now, and also pass me where the player spawned."

TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Hello world!")

"I want to show a message to this player. No sender should show up, the sender name (which doesn’t exist here anyways) should show up in the rgb colors 0, 0, 0 (black) and the message should be “Hello world!”"

end)

"That’s it."

Now save this file and go to your citmp-server.yml file. Add a - Welcome under AutoStartResources:. Save this too and start up your server.

Now if you join your server and you followed this tutorial correctly, you will see a “Hello world!” in white in the chat.

Congratulations! You just created a script that has been detected by the server and even did something.

What now?
The possibilities are infinite! There isn’t much documentation so you’ll have to do some research. This will be your new best friend for that.

Also have an eye in the server console and the F8 in-game console if your script doesn’t want to run.

Use this to quickly check if your syntax is correct.

Also take a look at scripts from other people to see how certain things are being achieved. I’ve got alot of public scripts here that you can take a look at to see how stuff works.

Good luck!

53 Likes

What a great idea! Some people have no idea how to even start scripting in LUA! This is such a great tutorial!

3 Likes

thank you @Mr.Scammer

Lua* :slight_smile: https://www.lua.org/about.html

Great tutorial! It would be so cool if you made more tutoris like this!

1 Like

How did you know to use this
[“playerSpawned”, function(spawn)]???

5 Likes

http://www.dev-c.com/nativedb/

2 Likes

Thanks, you provided me with exactly what I was looking for to get started!

Be careful, I think you accidently put a 5 at the end of the manifest, it’s actually 77731fab-63ca-442c-a67b-abc70f28dfa

1 Like

Where can we find event like “playerSpawned” ? There is nothing about spawning in Native DB

Yes its true i wanted to ask same

Yes because I found the event “chatMessage” in the folder chat, but there is a lot of events like “playerSpawned” that I can’t find. I suppose it’s a native function but idk where find it

Maybe we are bad in programation but we dont know it lmao

I know I’m bad, I’m a beginner :stuck_out_tongue: Maybe programation isn’t for us

This is not all of them but I found this searching the forums

  • sessionsFound
    Params: sessions

sessionJoining
Params: cur, max, hostName

sessionJoined
Params:

sessionJoinFailed
Params:

sessionHostFailed
Params: err

sessionHosted
Params:

sessionInitialized
Params:

sessionStateChanged
Params: state

getResourceInitFuncs
Params: isPreParse, add

onClientResourceStart
Params: res

onClientResourceStop
Params:

onResourceStop
Params: name

playerActivated
Params:

playerDropped
Params:

onPlayerJoining
Params: netId, name

onPlayerKilled
Params: playerId, attackerId, reason, position

onPlayerDied
Params: playerId, reason, position

onClientMapStart
Params:

onClientMapStop
Params:

onMapStart
Params:

getMapDirectives
Params: add

onClientGameTypeStart
Params:

------------------------ RegisterServerEvent ---------------------------

playerSpawned
Params:

For find -> “playerSpawned”, function(spawn) …
There is nothing on http://www.dev-c.com/nativedb (i think it’s for C# language)
And, where did you found this @pongo1231 and @xander1998

playerSpawned is not a native. It’s a exported function in the spawn manager I think

Yep I think too, but I can’t find it inside spawnmanager

Hello how to add a zone location script for command?

Can you help me for add this script in my folder please?

Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
for i=0,19 do
Citizen.Wait(1)
SetPedComponentVariation(GetPlayerPed(-1), 0, i, 0, 0)
exports.system:Notify(“id:” … i)
end
end
end)