Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

fivem scripting how would I create an event function that get run every 30-60 minutes…

  1. User

    fivem scripting how would I create an event function that get run every 30-60 minutes and creates a object that is synced for every player

  2. Assistant

    To create an event function that runs every 30-60 minutes and creates an object synced for every player in FiveM, you'll need to use both server-side and client-side scripting. Here's an example:

    1. First, create a server-side script, e.g., server.lua, and include the following code to define an event handler and a timer for running the event.
    local objectToSpawn = GetHashKey("prop_cone_float_1") -- Replace with desired object hash
    
    function spawnObject()
      local x, y, z = 0, 0, 0 -- Replace with desired object spawn coordinates
      TriggerClientEvent('spawnObjectAtCoords', -1, objectToSpawn, x, y, z)
    end
    
    local minute = 60000
    local minTime = 30 * minute
    local maxTime = 60 * minute
    
    function startTimer()
      local interval = math.random(minTime, maxTime)
      SetTimeout(interval, function()
        spawnObject()
        startTimer()
      end)
    end
    
    startTimer()
    
    1. Next, create a client-side script, e.g., client.lua, and include the following code to receive the event and spawn the object for every player.
    RegisterNetEvent('spawnObjectAtCoords')
    AddEventHandler('spawnObjectAtCoords', function(objectHash, x, y, z)
      local obj = CreateObject(objectHash, x, y, z, false, true, true)
      SetEntityAsMissionEntity(obj, true, true)
    end)
    
    1. Finally, ensure both the server-side and client-side scripts are included in your resource. Your resource's __resource.lua or fxmanifest.lua file should include the following:
    server_script 'server.lua'
    client_script 'client.lua'
    

    With these scripts in place, the server will run the spawnObject() function every 30-60 minutes, which will trigger the spawnObjectAtCoords event on the client side to create and sync the object for all players. Make sure to replace the placeholders with the hash of the desired object and the desired spawn coordinates.