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.
Inhaltsverzeichnis
Voraussetzungen
Bevor Sie beginnen, stellen Sie sicher, dass Sie über Folgendes verfügen:
- 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
- Navigate to Your Resources Directory:Locate the
Ressourcen
folder in your FiveM server directory. This is typically found at:bashCode kopierenyour-fivem-server-folder/resources/
- Create a New Resource Folder:Inside the
Ressourcen
Ordner, erstellen Sie einen neuen Ordner mit dem Namenrealtime
.bashCode kopierenyour-fivem-server-folder/resources/realtime/
- Navigieren Sie zum
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
)
- Erstellen
server.lua
:Inside therealtime
Ordner, erstellen Sie eine neue Datei mit dem Namenserver.lua
. - 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.
- RegisterNetEvent: Registers a network event named
Creating the Client-Side Script (client.lua
)
- Erstellen
client.lua
:Inside therealtime
Ordner, erstellen Sie eine neue Datei mit dem Namenclient.lua
. - 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.
- SetMillisecondsPerGameMinute: Defines how long an in-game minute lasts. Setting it to
Adding a Stop Functionality
To allow toggling the real-time synchronization on and off, we’ll add functions to start and stop the synchronization.
- Aktualisieren
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.
- Erstellen
fxmanifest.lua
:Inside therealtime
Ordner, erstellen Sie eine neue Datei mit dem Namenfxmanifest.lua
. - 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'
- 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.
- fx_version: Specifies the version of the FiveM manifest.
Starting the Resource on Your Server
- Edit Your Server Configuration:Open your server’s configuration file, typically named
server.cfg
. - Add the Resource to the Configuration:Add the following line to ensure the
realtime
resource starts with the server:rubyCode kopierenensure realtime
Notiz: If you’re usingStart
anstattsicherstellen
, you can use:start realtime
- 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
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!