Ir para o conteúdo principal
Version: latest - a1.64.x ⚖️

Valores da entidade

Como armazenar dados em Entidades acessíveis de qualquer lugar

Em nanos mundo é possível armazenar dados em entidades. Esses valores podem ser acessados por qualquer pacote e também você pode determinar se o valor será automaticamente sincronizado com os Clientes.

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. Como o valor só existirá do lado que você está configurando.

GetValue

After set, you can access any value from any entity using :GetValue(key, fallback) on any entity you want. Se você definir o valor a ser sincronizado em clientes, poderá obter os valores também no lado Cliente. 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.