Data - tayjay/SCriPt GitHub Wiki

Everything that your script has done so far is lost when the server restarts. If you want to keep data around between restarts, you need to save it to a file. This is called persistence. Here is an example of how you can save data to a file and load it back up when the server starts.

Saving

The plugin handles this automatically. Any change made to the table created by "Get" will be saved to a file in the Data folder. No manual saving is required.

Immutable Data

If you want to store data that cannot be changed, such as config data, make sure the name of the variable starts with an underscore _. This will make the data immutable, meaning it cannot be changed after it is set. For example:

config_example = SCriPt:Module('ConfigExample')
config_example.data = Data:Get('config_example')
config_example.data['_enabled'] = true -- This will be immutable

Example

-- Create a table to hold our script functions
storage_example = SCriPt:Module('StorageExample')
storage_example.data = Data:Get('storage_example')

function storage_example:load()
    -- Add the function to the event
    Events.Server.WaitingForPlayers:add(storage_example.onWaitingForPlayers)
end

function storage_example:unload()
    -- Cleanup the event
    Events.Server.WaitingForPlayers:remove(storage_example.onWaitingForPlayers)
end

function storage_example:onWaitingForPlayers()
    -- Increment the count whenever the server starts up
    if storage_example.data['count'] == nil then
        -- If the data doesn't exist, create it
      storage_example.data['count'] = 0
    end
    storage_example.data['count'] = storage_example.data['count'] + 1
    print("The server has been started " .. storage_example.data['count'] .. " times.")
    
end

Rules

There are some limitations on what you can store in the Store. Here are the rules:

  • The key must be a unique string
  • The value is always a "Prime Table" denoted with the $ prefix
  • A Prime Table can only contain the following types:
    • String
    • Number (int/float/double)
    • Boolean
    • Prime Table (with the same rules)
    • Nil