[HELP] TriggerClientEvent on ResourceStart

Hello guys, I have this little piece of code:

__resource.lua

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

client_script 'client.lua'
server_script 'server.lua'

.
client.lua

RegisterNetEvent('testEvent')
AddEventHandler('testEvent', function(str)
    TriggerEvent('chatMessage', 'SERVER: ', {77, 161, 98}, str .. ' spawned')
    vehicle = GetHashKey(str)
	RequestModel(vehicle)
    cveh = CreateVehicle(vehicle, 73.348, -272.178, 47.712, 168.64, true, true)
end)

server.lua

AddEventHandler('onResourceStart', function(resourceName)
    if (GetCurrentResourceName() == resourceName) then
        TriggerClientEvent('testEvent', 1, 'vacca')
        print('Resource started. :)')
    end
end)

The idea behind is to spawn a vehicle at those coordinates when the first player is connected. But for some reason the resource doesn’t trigger the client event. It prints me in the console “Resource started :)” but is not triggering the event. What am I doing wrong?
I forgot to mention that I’ve created a command to trigger the event and it works without any problem.

1 Like

Try to wait at least one tick before you trigger the event.

Sidenote: Try using chat:addMessage as chatMessage is deprecated

Great! It works, I’ve added Wait(1000). Thank you!
Also, thank you for the info about chatMessage, I’m not planning to use it inside of this resource, it is only for debugging for the moment.

Now, the problem is vehicle spawning, it triggers the event as it should, but the vehicle is not spawning.

Try with this server native:

GetResourceState

https://runtime.fivem.net/doc/natives/#_0x4039B485

Here’s an example for a vehicle spawner from the “Create your first script” of the docs. Perhaps this will help (maybe you didn’t load the model or so)

Yes, I already tried this when I started learning about FiveM and how it works.
Right now I’m struggling with a system to spawn persistent faction vehicles(police, mafias, etc) when server boots up and it’s harder than I thought.