Hi, I've been asking this question for 4 times and this is the 5th one so please help me. So basically I added bool values to the folder so I could access the players inventory later in the game and the problem is that it isn't saving.
Datastore2 script:
01 | local DataStore 2 = require( 1936396537 ) |
02 | local default = { "Wooden Sword" , "Wooden Bow" , "Wooden Spear" } |
03 | game.Players.PlayerAdded:Connect( function (plr) |
04 | local dataweap = DataStore 2 ( "Weapons" ,plr) |
05 |
06 | local fol = Instance.new( "Folder" ) |
07 | fol.Parent = plr |
08 | fol.Name = "Weapons" |
09 | dataweap:Get(default) |
10 | for i,v in pairs (dataweap:Get()) do |
11 | local tag = Instance.new( "BoolValue" ) |
12 | tag.Parent = fol |
13 | tag.Name = v |
14 | end |
15 |
Giver script:
01 | local par = script.Parent |
02 | local cool = false |
03 | par.Touched:Connect( function (hit) |
04 | if hit.Parent:FindFirstChild( "Humanoid" ) and cool = = false then |
05 | cool = true |
06 | if game.Players:GetPlayerFromCharacter(hit.Parent) then |
07 | local name = game.Players:GetPlayerFromCharacter(hit.Parent) |
08 | local Datastore 2 = require( 1936396537 ) |
09 | local dataweap = Datastore 2 ( "Weapons" ,name) |
10 | local weap = game.ReplicatedStorage.Weapons:GetChildren() |
11 | local ran = weap [ math.random( 1 ,#weap) ] |
12 | print (ran) |
13 | table.insert(dataweap, ran.Name) |
14 | for i,v in pairs (dataweap:Get()) do -- Just to check what is in the data |
15 | print (v) |
The giver gives a player random weapons when they touch it. So this actually print out:
1 | -- Magic Staff |
2 |
3 | -- Wooden Sword |
4 | -- Wooden Bow |
5 | -- Wooden Spear |
So I think that there is some problem with the table.insert thing in the giver script or is there a different way to put string values into a datastore2 table? Help please, Thank you
This is a good question, and a lot of people ask this... so hopefully, I can help you out.
Let's start with an example, setting up a DataStore.
01 | local Players = game:GetService( "Players" ) |
02 |
03 | local DataStore 2 = require( 1936396537 ) |
04 | DataStore 2. Combine( "DATA" , "weapons" ) |
05 |
06 | local default_data = { "Wooden Sword" , "Wooden Bow" , "Wooden Spear" } |
07 |
08 | Players.PlayerAdded:Connect( function (player) |
09 | local weaponsStore = DataStore 2 ( "weapons" , player) |
10 |
11 | -- Get Data & Set Default |
12 | local weapons = weaponsStore:Get(default_data) |
13 | end ) |
Now what this did, is we created the DataStrore, and then we fetched the data and set a default value if no data has been saved under that key... Now, to answer the question about saving tables, rather than using the :Get
function, we would use the :Set
function. Here is an example:
1 | local new_data = { "Stone Sword" , "Wooden Bow" , "Wooden Spear" } |
2 | weaponsStore:Set(new_data) |
I hoped this answer your question, and if it didn't, respond to this post and I will do my best to assist you further! Also, here is a link to the DataStore2 documentation, this should be able to answer all your questions.