Skip to main content
Version: latest - a1.64.x ⚖️

Entitätswerte

Wie man Daten in allen verfügbaren Entitäten speichert

In der nanos Welt ist es möglich, Daten auf Entitäten zu speichern. Auf diese Werte kann jedes Paket zugreifen und Sie können auch feststellen, ob der Wert automatisch mit den Clients synchronisiert wird.

In Base Entity page we can find the definitions of :SetValue() and :GetValue() methods. Also it is possible to store Values globally on Client and Server Static Classes with the same methods!

SetValue

In any entity, you can use the method :SetValue(key, value, sync) to set any kind of value on that entity. Example:

Server/Index.lua
-- Sets a synchronized 'my_value' value
my_player:SetValue("my_value", 100, true)
tip

It is possible to store any kind of value, except functions.

If you pass sync as true, that value will be automatically synchronized with all clients. Note that syncis an optional parameter only available on the Server side.

note

Client.SetValue() and Server.SetValue() won't have the last parameter sync. Da der Wert nur auf der Seite vorhanden ist, die Sie einstellen.

GetValue

After set, you can access any value from any entity using :GetValue(key, fallback) on any entity you want. Wenn Sie den Wert festlegen, der auf Clients synchronisiert werden soll, können Sie die Werte auch auf der Client-Seite abrufen. You can pass an aditional parameter fallback which will be returned if the key doesn't exist!

-- Gets 'my_value' value, or returns '0' if no value is set
local my_value = my_player:GetValue("my_value", 0)
caution

Be aware that storing entities itself will not nullify the value if the entity is destroyed, so it is a good practice to validate if values with :IsValid() after retrieving them.