[HELP] Understanding the example script

I’m having trouble understanding some of the concepts in the example game mode from the fivem documentation. I’ve annotated every line with what I’m having trouble with. If anyone could help I would greatly appreciate it.

-- I understand this.
local spawnPos = vector3(686.245, 577.950, 130.461)

-- In plain english this sounds like:
-- When event 'onClientGameTypeStart' is triggered, execute the following code.
-- However, why are the words function() written as an argument for AddEventHandler?
AddEventHandler('onClientGameTypeStart', function()
-- is exports an object?
-- are we calling a function of that object or overriding it?
-- why is function() here again?
    exports.spawnmanager:setAutoSpawnCallback(function()
-- this line makes me feel like we're overriding it, if so this makes sense but the syntax is confusing.
-- is the following code a function() that we're passing as an argument in the function call 
-- if so why can't we write the function somewhere else and just call it in setAutoSpawnCallback instead of writing it somehow within this line? I don't even understand what I'm saying this is confusing as hell.
setAutoSpawnCallback?
        exports.spawnmanager:spawnPlayer({
            x = spawnPos.x,
            y = spawnPos.y,
            z = spawnPos.z,
            model = 'a_m_m_skater_01'
-- function() again lol. The docs say this is a callback function but I don't understand what that means.
        }, function()
            TriggerEvent('chat:addMessage', {
                args = { 'Welcome to the party!~' }
-- this probably stems from a lack of lau syntax knowledge but idk where these braces/ends connect to
            })
        end)
    end)
-- I assume these two lines are calling a function of the object exports. If so I understand this.
    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end)

What exactly do you want to change?

I just don’t understand what’s going on.
As I posted this I’ve edited it a couple times because I think I just realized something I was missing.

There’s nothing I’d like to change, I’d just like to fully understand each line.

Do you understand the basics and have general knowledge of coding or?

coding yes, lua not so much.

These might help:

https://www.lua.org/pil/1.html

https://www.tutorialspoint.com/lua/

although the way i learned was editing scripts. but those videos/links might help you i am still trying to learn which is why i’m probably going to end up googling LUA classes,or videos.

^ that should help you a ton.

1 Like

Thank you I will watch these.

1 Like

Hopefully the videos will help me understand but I’m still open to help with each individual line of that script I posted. I hope my annotations of the script make sense.

infact i haven’t even watched those videos give me about 2 hours to learn lua lol :joy: hopefully half the stuff they talk about helps me write scripts from scratch lol maybe even help me make my own framework.

@louie904 If i’m correct, you seem to have issues with the concept of callbacks?

When you pass a function as an argument to another function, the function that you’re calling can call the function you pass it as an argument at a certain point in time. This would then execute the code you wrote in the function that you passed as an argument. The function you pass is called a callback function.

Here’s an example:

Example
function PrintHelloAndCallCallback(callback)
	print("Hello")
	callback()
end

PrintHelloAndCallCallback(function()
	print("World")
end)

Its often used when dealing with asynchronous data too, here is an example but theres many youtube videos out there discussing it. https://www.youtube.com/watch?v=xHneyv38Jro

Thank you, I feel much closer to understanding callbacks.
If you could do me a huge favor I’m sure I’ll finally get it.
Can you write the following script without using callbacks?

local spawnPos = vector3(686.245, 577.950, 130.461)

AddEventHandler('onClientGameTypeStart', function()
    exports.spawnmanager:setAutoSpawnCallback(function()
        exports.spawnmanager:spawnPlayer({
            x = spawnPos.x,
            y = spawnPos.y,
            z = spawnPos.z,
            model = 'a_m_m_skater_01'
        }, function()
            TriggerEvent('chat:addMessage', {
                args = { 'Welcome to the party!~' }
            })
        end)
    end)

    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end)```

I don’t really use Lua, nor do I know much about it. However, if they’re using callbacks in the example that probably is done for good reason and theres likely no way around them. If anyone knows feel free to correct me.

What you could do however is clean it up a bit by seperating the function definitions. Take a look:

Example
local spawnPos = vector3(686.245, 577.950, 130.461)

AddEventHandler('onClientGameTypeStart', onClientGameStart)

function onClientGameStart()
    exports.spawnmanager:setAutoSpawnCallback(autoSpawnCallback)
    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end

function autoSpawnCallBack()
    exports.spawnmanager:spawnPlayer({
        x = spawnPos.x,
        y = spawnPos.y,
        z = spawnPos.z,
        model = 'a_m_m_skater_01'
    }, sendWelcomeMessage)
end

function sendWelcomeMessage()
    TriggerEvent('chat:addMessage', {
        args = { 'Welcome to the party!~' }
    })
end

I have not tested this, but as far as I can tell this should do exactly the same thing, but it is now more readable.

I actually went ahead a wrote something after I posted that reply and its pretty similar lol.
I think I made a mistake on line 4 with the “theAutoSpawn()” which should be “theAutoSpawn” according to the way you wrote it.
My code didn’t work but I think that was the reason. I will try again now.

local spawnPos = vector3(686.245, 577.950, 130.461)

AddEventHandler('onClientGameTypeStart')
    exports.spawnmanager:setAutoSpawnCallback(theAutoSpawn())
    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end

function theAutoSpawn()
    exports.spawnmanager:spawnPlayer({
        x = spawnPos.x,
        y = spawnPos.y,
        z = spawnPos.z,
        model = 'a_m_m_skater_01'
    })

    TriggerEvent('chat:addMessage', {
        args = { 'Welcome to the party!~' }
    })
end

I can’t connect to the server while either resource is running but the example code still works perfectly.

I dont understand this sentence ^.

This should provide you with all the native functions you can call so you dont have to worry about the correct spelling etc. There should also be something for intellij I believe but im not sure.

Sorry I didn’t type the previous reply clearly.
However, I got it to work with the following code!

spawnPos = vector3(686.245, 577.950, 130.461)

AddEventHandler('onClientGameTypeStart', function()
    exports.spawnmanager:setAutoSpawnCallback(theAutoSpawn)
    exports.spawnmanager:setAutoSpawn(true)
    exports.spawnmanager:forceRespawn()
end)

function theAutoSpawn()
    exports.spawnmanager:spawnPlayer({
        x = spawnPos.x,
        y = spawnPos.y,
        z = spawnPos.z,
        model = 'a_m_m_skater_01'
    }, function ()
        TriggerEvent('chat:addMessage', {
            args = { 'Welcome to the party!~' }
        })
    end)
end```