# Authoritative Storage

**URL:** <https://forum.heroiclabs.com/t/authoritative-storage/158>\
**Category:** Local Setup\
**Created:** [August 24, 2019, 7:25am UTC](https://forum.heroiclabs.com/t/authoritative-storage/158 "2019-08-24T07:25:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![terahardstudios](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/terahardstudios/32/43_2.png) [@terahardstudios](https://forum.heroiclabs.com/u/terahardstudios)\
**Post date:** [August 24, 2019, 7:25am UTC](https://forum.heroiclabs.com/t/authoritative-storage/158/1 "2019-08-24T07:25:34Z")

</div>

So I’ve tried a few permutations and for some reason what I’m doing doesn’t work.  
I am running an authoritative server node and I am trying to save the state upon when a user is leaving in match\_leave.

```
    print('Saving Map ' .. nk.json_encode(state) )

	for _, r in ipairs(state.Map.Buildings) do
	  local message = ("Saving Position = %q, Type = %q"):format(r.Position, r.Type)
	  nk.logger_info(message)
	end

	local new_objects = 
	{
		{collection = "save", key = "save1", user_id = nil, value = {nk.json_encode(state)}, permission_read = 0, permission_write = 0}
	}
	nk.storage_write( new_objects )

	local records = nk.storage_list( nil, "save", 10, "")
	for _, r in ipairs(records) do
		
	  local message = ("read: %q, write: %q, value: %q"):format(r.permission_read, r.permission_write, nk.json_encode(r.value) )
	  nk.logger_info(message)
	end

```

The result log is:

> Saving Map {“Map”:{“Buildings”:[{“Position”:{“x”:-2.1180593967437744,“y”:1.804999828338623,“z”:0},“Type”:0},{“Position”:{“x”:-4.390469074249268,“y”:1.8701850175857544,“z”:0},“Type”:0},{“Position”:{“x”:-6.561397075653076,“y”:2.451883316040039,“z”:0},“Type”:0},{“Position”:{“x”:-8.892043113708496,“y”:3.076378345489502,“z”:0},“Type”:0}]},“label”:{“total\_joined”:2,“visibility”:“private”},“presences”:{“58fba184-7c73-4b74-ad6b-ce53699fe381”:{“node”:“nakama1”,“session\_id”:“58fba184-7c73-4b74-ad6b-ce53699fe381”,“user\_id”:“b913636f-077e-4b66-b607-949fcec99503”,“username”:“TqGjzLtzOc”}}}

> nakama | {“level”:“info”,“ts”:“2019-08-24T07:10:46.928Z”,“msg”:“Saving Position = "table: 0xc0012d7e60", Type = "0"”,“mid”:“4283cb13-e79c-42e9-a505-ee5734c5ebd1”}  
> nakama | {“level”:“info”,“ts”:“2019-08-24T07:10:46.928Z”,“msg”:“Saving Position = "table: 0xc001192900", Type = "0"”,“mid”:“4283cb13-e79c-42e9-a505-ee5734c5ebd1”}  
> nakama | {“level”:“info”,“ts”:“2019-08-24T07:10:46.928Z”,“msg”:“Saving Position = "table: 0xc000a14120", Type = "0"”,“mid”:“4283cb13-e79c-42e9-a505-ee5734c5ebd1”}  
> nakama | {“level”:“info”,“ts”:“2019-08-24T07:10:46.928Z”,“msg”:“Saving Position = "table: 0xc000b4f7a0", Type = "0"”,“mid”:“4283cb13-e79c-42e9-a505-ee5734c5ebd1”}

> nakama | {“level”:“info”,“ts”:“2019-08-24T07:10:46.938Z”,“msg”:“read: "0", write: "0", value: "{}"”,“mid”:“4283cb13-e79c-42e9-a505-ee5734c5ebd1”}

It seems that it never saves and the value is {}.  
I would say the only thing unique to the docs is that the user owning this save is always nil since I want the server to be the owner. Any ideas?

---

<div class="post-metadata">

**Author:** ![GroovyAntoidDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/groovyantoiddev/32/39_2.png) [@GroovyAntoidDev](https://forum.heroiclabs.com/u/GroovyAntoidDev)\
**Post date:** [August 24, 2019, 9:59am UTC](https://forum.heroiclabs.com/t/authoritative-storage/158/2 "2019-08-24T09:59:12Z")

</div>

> [@terahardstudios](#):
>
> value = {nk.json\_encode(state)}

Perhaps instead try directly using `value = state`.

---

<div class="post-metadata">

**Author:** ![terahardstudios](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/terahardstudios/32/43_2.png) [@terahardstudios](https://forum.heroiclabs.com/u/terahardstudios)\
**Post date:** [August 24, 2019, 10:56am UTC](https://forum.heroiclabs.com/t/authoritative-storage/158/3 "2019-08-24T10:56:41Z")

</div>

I swear to god I tried this before and value = { state } and it hadn’t worked…  
Does object ids mean “objects” only rather than an array with anything, in this example a string?

> object\_ids `[]*runtime.StorageWrite` table A table / array of object identifiers to be fetched.

---

<div class="post-metadata">

**Author:** ![GroovyAntoidDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/groovyantoiddev/32/39_2.png) [@GroovyAntoidDev](https://forum.heroiclabs.com/u/GroovyAntoidDev)\
**Post date:** [August 24, 2019, 12:42pm UTC](https://forum.heroiclabs.com/t/authoritative-storage/158/4 "2019-08-24T12:42:48Z")

</div>

> [@terahardstudios](#):
>
> Does object ids mean “objects” only rather than an array with anything, in this example a string?

`Object ids` in storage\_write refers to an array of objects to store, and in this case it was an array with one object only, but it can be several objects like so:

> [@terahardstudios](#):
>
> local new\_objects = {  
> {  
> collection = “save”, key = “save1”, user\_id = nil, value = state1, permission\_read = 0, permission\_write = 0},  
> collection = “save”, key = “save2”, user\_id = nil, value = state2, permission\_read = 0, permission\_write = 0},  
> collection = “save”, key = “save3”, user\_id = nil, value = state3, permission\_read = 0, permission\_write = 0}  
> }  
> nk.storage\_write( new\_objects )

---

<div class="post-metadata">

**Author:** ![terahardstudios](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/terahardstudios/32/43_2.png) [@terahardstudios](https://forum.heroiclabs.com/u/terahardstudios)\
**Post date:** [August 24, 2019, 1:09pm UTC](https://forum.heroiclabs.com/t/authoritative-storage/158/5 "2019-08-24T13:09:15Z")

</div>

> [@GroovyAntoidDev](#):
>
> `Object ids` in storage\_write refers to an array of objects to store

I see, but a string is an object too and it didn’t work
