pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 08:14 PM |
I've tried for at least 2 days and I tried several different methods and I'm getting quite frustrated! It seems so simple, but I just can't figure it out!
I'm trying to use data store for multiple values using a table. I can create the table and the contents, but I don't know how to save/load all the contents inside the table.
Can someone explain how to do this? |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 08:18 PM |
.........did you even read what i posted
datastore:SetAsync('KEY',TABLE) datastore:GetAsync('KEY')
|
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 08:29 PM |
--gets DataStoreService
game.Players.PlayerAdded:connect(function(p) local key = "player-" ..p.userId --variables that go in the table local data = {level.Value, exp.Value, gold.Value, chapter.Value} ds:GetAsync(key) print("Saving data...") ds:SetAsync(key, data) print("Saved!") end)
game.Players.PlayerRemoving:connect(function(p) local key = "player-" ..p.userId --copied all the variables and the table print("Saving data...") ds:SetAsync(key, data) print("Saved!") end)
Doesn't save for some reason. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 08:30 PM |
| that's because you reset the data every time the player is added, and you don't properly load the values into level, exp, gold, and chapter |
|
|
| Report Abuse |
|
|
| |
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 08:32 PM |
I tried making it a condition that if they actually HAD data it would load it, or else it would just set everything to 0 but...
...I forgot how. c: |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 08:32 PM |
| and I'm error-free, my friend. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 08:32 PM |
| if GetAsync returns nil and doesn't error then that means the player has no data |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:16 PM |
| How do I properly load the values? I did several experiments to no avail. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 09:18 PM |
ds:GetAsync(key)
This does absolutely nothing. You need to do something to the tune of:
data = ds:GetAsync(key)
GetAsync returns a value, and doesn't implicitly set a variable to it. |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:20 PM |
| I already assigned a variable to ds:GetAsync(key) during one of my experiments. Now I don't know what to do with it. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 09:23 PM |
Whatever you get from :GetAsync() is going to be an exact copy of what you put in (for the most part). Treat it as that; it isn't hard.
For example, if the index for Level was 1 in the table, you'd do
data = (getasync call) local L = data[1]
And then you'd have the player's level in the local variable L. |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:25 PM |
| so I have to do data[1], data[2], data[3], etc. for each value? |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 09:27 PM |
Well, technically, if the order is in this fashion:
data = {level, exp, money}
You could do this, respectively:
local level, exp, money = unpack(data)
It's a better and cleaner way of doing it rather than using tons of indices. |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:34 PM |
Look, you're a great scripter and I don't think I'll ever be as good as you, but I just don't know where the pieces of the puzzle fit in. I'm probably gonna look really stupid with my code right now, but I just want help so I can get it working.
--getting DataStoreService game.Players.PlayerAdded:connect(function(p) --key --variables --table local load = ds:GetAsync(key) if load then local level, exp, gold, chapter = unpack(data) level = load[1] exp = load[2] gold = load[3] chapter = load[4] --I have no idea what I'm doing here. I just experimented. else local newdata = {data} print("Saving data...") ds:SetAsync(key, newdata) print("Saved!") end end)
--basically saves the game when player leaves
I don't know where I went wrong. There's nothing in the output. Whenever I leave and rejoin the values are all 0. I'm sorry if I'm giving anyone a hard time, but I'm having a hard time myself and I feel stupid. :( |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 09:36 PM |
Well, I presume you're storing the player data with IntValues and such, right?
Are you setting the values of those IntValues after you load the data, based on the loaded values? |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 09:39 PM |
Wait, nevermind. I don't mean literally do unpack(data).
You should place your loaded table in place of 'data'. So, unpack(load). And then remove the level = load[1] and so on.
Also, in your case, if level, exp, chapter, and gold are already defined earlier in the function, remove the 'local', as they'll disappear when the if statement exits. |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:39 PM |
I am indeed using IntValues.
I'm trying to simply load up data (if the person has data) or create new data if the person doesn't have data and I want it all to save when the player leaves so when they rejoin it loads up their data again. |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:46 PM |
| It's making me hurt every time I see the zeroes in the place of where the loaded data is supposed to be. I don't think this code will ever work. I wasted 2 days of spring break for this crap. I tried what you said. Nothing in the output, no saving/loading, no avail. |
|
|
| Report Abuse |
|
|
pwnion
|
  |
| Joined: 03 Jan 2017 |
| Total Posts: 117 |
|
|
| 18 Mar 2017 09:50 PM |
| Also I'm getting mad because stupid ROBLOX forums won't let me post my full code. All the :WaitForChild("")'s are censored and if there are too many censors it won't post. >:( |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2017 09:58 PM |
I wrote a 50-line example to show that data storage doesn't necessarily need to be hard. It's uncopylocked.
Some notes on this:
The variable 'C' is just a sort-of compact version of Instance.new(). coroutine.wrap() is used for threading behavior, to implement an autosave feature. This allows the script to continue execution whilst still running a loop. Save() is its own function, due to the fact that it is used by both the disconnect event and the autosave loop. I increment the Level variable just to show that it works.
Game here: https://www.roblox.com/games/701260120/ |
|
|
| Report Abuse |
|
|