Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to insert value into datastore Array?

Whenever I run the following function it resets the player's inventory and makes them have only the only one item and that described in the function. What is the problem?

function AddInventory(plyr,Item)
	
	table.insert(PlayerList, plyr.UserId, {Slots = 30, Items = {Item}})

	DataStore.PostDataStore("Inventory7", plyr.UserId, PlayerList[plyr.UserId])
	
	
end

--For example, if I ran this two times in a row
--Add Inventory(plyr, WoodenSword)
--Add Inventory(plyr, BasicKatana)
--The Basic Katana would only show in my inventory

1 comment

0

i think the mistake is on line #3, it should probably be table.insert(PlayerList[plyr.UserId], {Slots = 30, Items = {Item}})

1 answer

✓ Accepted answer

To insert/modify data in a DataStore array you must use SetAsync, UpdateAsync and RemoveAsync.

SetAsync creates a new value and modifies it to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:SetAsync("ExamplePlayer", 50)

This creates a new value ExamplePlayer and sets it's corresponding value to 50.

UpdateAsync modifies an existing value to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:UpdateAsync("ExamplePlayer", 50)

This changes ExamplePlayer's corresponding value to 50

RemoveAsync removes a value in the array.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:RemoveAsync("ExamplePlayer")

This removes ExamplePlayer and its corresponding value

For more see Data Stores.

1 comment

0

The problem isn't that I can't change the value of the data store. The problem is whenever I change the value it overwrites everything.

This discussion is preserved as it was. New answers, comments, and votes are closed.