Script LUA, Mods FiveM grátis, Scripts FiveM grátis

Sincronização em tempo real FiveM

Script em tempo real FiveM

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.


Pré-requisitos

Antes de começar, certifique-se de ter o seguinte:

  • 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 recursos 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 recursos folder, create a new folder named realtime.bashCode kopierenyour-fivem-server-folder/resources/realtime/
  3. Navegue até o realtime Folder:This folder will contain all the necessary scripts and configuration files for the real-time synchronization.

Creating the Server-Side Script (servidor.lua)

  1. Criar servidor.lua:Inside the realtime pasta, crie um novo arquivo chamado servidor.lua.
  2. Add the Following Code to servidor.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 (cliente.lua)

  1. Criar cliente.lua:Inside the realtime pasta, crie um novo arquivo chamado cliente.lua.
  2. Add the Following Code to cliente.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. Atualizar cliente.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. Criar fxmanifest.lua:Inside the realtime pasta, crie um novo arquivo chamado 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:
    • versão_fx: Specifies the version of the FiveM manifest. cerúleo 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 servidor.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 garantir, you can use: start realtime
  3. Save and Restart Your Server:After saving the changes to servidor.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. servidor.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. cliente.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!

autor-avatar

Sobre Luke

Eu sou Luke, sou um gamer e adoro escrever sobre FiveM, GTA e roleplay. Eu administro uma comunidade de roleplay e tenho cerca de 10 anos de experiência em administração de servidores.

Deixe um comentário