FiveM Real Time Script

FiveM Real-Time Sync

This tutorial will guide you through creating a FiveM script that synchronizes the in-game clock with real-world time. This ensures that the game environment reflects the actual time, enhancing realism for players.

We’ll cover both server-side and client-side scripting, adding functionality to start and stop the synchronization, and setting up the resource for your FiveM server.


Prerequisites

Before you begin, ensure you have the following:

  • FiveM Server Access: You need administrative access to your FiveM server to add scripts.
  • Basic Knowledge of Lua: Familiarity with Lua scripting will help you understand and customize the script.
  • Text Editor: Software like Visual Studio Code, Sublime Text, or Notepad++ for editing script files.

Setting Up the Resource Folder

  1. Navigate to Your Resources Directory:Locate the resources folder in your FiveM server directory. This is typically found at:bashCode kopierenyour-fivem-server-folder/resources/
  2. Create a New Resource Folder:Inside the resources folder, create a new folder named realtime.bashCode kopierenyour-fivem-server-folder/resources/realtime/
  3. Navigate to the realtime Folder:This folder will contain all the necessary scripts and configuration files for the real-time synchronization.

Creating the Server-Side Script (server.lua)

  1. Create server.lua:Inside the realtime folder, create a new file named server.lua.
  2. Add the Following Code to server.lua:luaCode kopierenRegisterNetEvent("realtime:event") AddEventHandler("realtime:event", function() local hour = tonumber(os.date("%H")) local minute = tonumber(os.date("%M")) local second = tonumber(os.date("%S")) TriggerClientEvent("realtime:event", source, hour, minute, second) end) Explanation:
    • RegisterNetEvent: Registers a network event named realtime:event.
    • AddEventHandler: Defines what happens when the realtime:event is triggered.
    • os.date: Retrieves the current system time (hour, minute, second).
    • TriggerClientEvent: Sends the time data to the client that triggered the event.

Creating the Client-Side Script (client.lua)

  1. Create client.lua:Inside the realtime folder, create a new file named client.lua.
  2. Add the Following Code to client.lua:luaCode kopieren-- Set the duration of one in-game minute in milliseconds SetMillisecondsPerGameMinute(60000) -- 60,000 ms = 1 real-world minute RegisterNetEvent("realtime:event") AddEventHandler("realtime:event", function(hour, minute, second) NetworkOverrideClockTime(hour, minute, second) end) -- Trigger the server event to initiate synchronization TriggerServerEvent("realtime:event") Explanation:
    • SetMillisecondsPerGameMinute: Defines how long an in-game minute lasts. Setting it to 60000 makes 1 in-game minute equal to 1 real-world minute.
    • RegisterNetEvent & AddEventHandler: Listens for the realtime:event from the server and updates the in-game clock accordingly.
    • NetworkOverrideClockTime: Overrides the in-game clock to match the real-world time received from the server.
    • TriggerServerEvent: Initiates the synchronization by triggering the server event.

Adding a Stop Functionality

To allow toggling the real-time synchronization on and off, we’ll add functions to start and stop the synchronization.

  1. Update client.lua with Stop Functionality:luaCode kopierenlocal syncActive = true local syncThread = nil -- Function to start synchronization function StartRealTimeSync() if not syncActive then syncActive = true syncThread = CreateThread(function() while syncActive do TriggerServerEvent("realtime:event") Wait(60000) -- Wait for 1 minute before next sync end end) end end -- Function to stop synchronization function StopRealTimeSync() if syncActive then syncActive = false if syncThread then -- In Lua, there's no direct way to kill a thread. -- Using a flag to exit the loop effectively stops the thread. syncThread = nil end end end RegisterNetEvent("realtime:event") AddEventHandler("realtime:event", function(hour, minute, second) if syncActive then NetworkOverrideClockTime(hour, minute, second) end end) -- Start synchronization on resource start StartRealTimeSync() -- Example: Command to toggle synchronization RegisterCommand("toggleTimeSync", function() if syncActive then StopRealTimeSync() print("Real-time synchronization stopped.") else StartRealTimeSync() print("Real-time synchronization started.") end end, false) Explanation:
    • syncActive: A flag to determine if synchronization is active.
    • StartRealTimeSync: Initiates a loop that requests time updates from the server every minute.
    • StopRealTimeSync: Stops the synchronization by setting the flag to false.
    • RegisterCommand: Adds a command (/toggleTimeSync) that players can use to toggle synchronization on or off.

Creating the Resource Manifest (fxmanifest.lua)

Every FiveM resource requires a manifest file that defines its metadata and dependencies.

  1. Create fxmanifest.lua:Inside the realtime folder, create a new file named fxmanifest.lua.
  2. Add the Following Code to fxmanifest.lua: fx_version 'cerulean' game 'gta5' author 'YourName' description 'Real-Time Synchronization Script for FiveM' version '1.0.0' server_script 'server.lua' client_script 'client.lua'
  3. Explanation:
    • fx_version: Specifies the version of the FiveM manifest. cerulean is the latest as of writing.
    • game: Indicates the game the resource is for (gta5).
    • author, description, version: Metadata about your resource.
    • server_script & client_script: Specifies the server and client scripts to be loaded.

Starting the Resource on Your Server

  1. Edit Your Server Configuration:Open your server’s configuration file, typically named server.cfg.
  2. Add the Resource to the Configuration:Add the following line to ensure the realtime resource starts with the server:rubyCode kopierenensure realtime Note: If you’re using start instead of ensure, you can use: start realtime
  3. Save and Restart Your Server:After saving the changes to server.cfg, restart your FiveM server to load the new resource.

Full Resource Download

For convenience, here’s the complete set of files you need to create for the realtime resource.

1. server.lua

RegisterNetEvent("realtime:event")
AddEventHandler("realtime:event", function()
    local hour = tonumber(os.date("%H"))
    local minute = tonumber(os.date("%M"))
    local second = tonumber(os.date("%S"))
    TriggerClientEvent("realtime:event", source, hour, minute, second)
end)

2. client.lua

local syncActive = true
local syncThread = nil

-- Function to start synchronization
function StartRealTimeSync()
    if not syncActive then
        syncActive = true
        syncThread = CreateThread(function()
            while syncActive do
                TriggerServerEvent("realtime:event")
                Wait(60000) -- Wait for 1 minute before next sync
            end
        end)
    end
end

-- Function to stop synchronization
function StopRealTimeSync()
    if syncActive then
        syncActive = false
        if syncThread then
            -- In Lua, threads are cooperative; setting syncActive to false will stop the loop
            syncThread = nil
        end
    end
end

RegisterNetEvent("realtime:event")
AddEventHandler("realtime:event", function(hour, minute, second)
    if syncActive then
        NetworkOverrideClockTime(hour, minute, second)
    end
end)

-- Start synchronization on resource start
StartRealTimeSync()

-- Example: Command to toggle synchronization
RegisterCommand("toggleTimeSync", function()
    if syncActive then
        StopRealTimeSync()
        print("Real-time synchronization stopped.")
    else
        StartRealTimeSync()
        print("Real-time synchronization started.")
    end
end, false)

3. fxmanifest.lua

fx_version 'cerulean'
game 'gta5'

author 'YourName'
description 'Real-Time Synchronization Script for FiveM'
version '1.0.0'

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

Full Script

Here you can download the script we’ve just created:

https://github.com/HiFiveM/fivem-realtime/archive/refs/heads/main.zip

Github

You’ve successfully created a FiveM resource that synchronizes the in-game clock with real-world time. This script enhances the gaming experience by ensuring that the game environment reflects the actual time, adding a layer of realism for players.

You can further customize the script by adjusting synchronization intervals, adding more commands, or integrating it with other server features.

Feel free to expand upon this foundation to suit your server’s unique needs!

Leave a Reply

Your email address will not be published. Required fields are marked *


Direct Download

For all Free and Paid Mods

Easy 30 days returns

30 days money back guarantee

International Warranty

Offered in the country of usage

100% Secure Checkout

MasterCard / Visa

Sale Sale Sale

SAVE UP TO 80%

Enter Your Email To Claim Your Discount!
GET MY DISCOUNT OFFER
Hurry up, Limited Deals Available!
close-link