Can someone help me with DataStore?
So, I made this out of what CodeTheorem gave me. When I say /save, it prints Saved. When I say /load, it prints loaded. But, when I load after saving, my cash gets set to 0 and doesn't continue to increase. After reading through the code, to me it seems like it's saving the names of the Values and not the actual values of them. When I load from the Data Store, I want it to set each Value to the value they were at when I saved.
01 | local KEY_PREFIX = "userid_" |
02 | local ds = game:GetService( "DataStoreService" ):GetDataStore( "PlayerValuesDataStore" ) |
03 | local profilesHolder = game.Workspace.Profiles |
04 | function saveToDataStore(plr) |
05 | local key = KEY_PREFIX .. tostring (plr.userId) |
06 | local profile = profilesHolder [ plr.Name ] |
08 | for i,v in ipairs (profile:GetChildren()) do |
09 | value [ v.Name ] = v.Value |
11 | ds:SetAsync(key, value) |
14 | function loadFromDataStore(plr) |
15 | local key = KEY_PREFIX .. tostring (plr.userId) |
16 | local data = ds:GetAsync(key) |
17 | local profile = profilesHolder [ plr.Name ] |
18 | local profileItems = profile:GetChildren() |
19 | for i,v in ipairs (profile:GetChildren()) do |
20 | profileItems [ i ] .Value = v |
24 | game.Players.PlayerAdded:connect( function (player) |
25 | player.Chatted:connect( function (msg) |
27 | if msg = = "/save" then |
28 | saveToDataStore(player) |
30 | elseif msg = = "/load" then |
31 | loadFromDataStore(player) |
And here's the script that creates the values.
01 | game.Players.PlayerAdded:connect( function (player) |
02 | Profile = Instance.new( "Model" ) |
03 | Profile.Parent = script.Parent |
04 | Profile.Name = (player.Name) |
05 | Cash = Instance.new( "NumberValue" ) |
06 | Cash.Parent = script.Parent [ player.Name ] |
11 | a.Parent = script.Parent [ player.Name ] |
15 | a.Parent = script.Parent [ player.Name ] |
18 | a.Parent = script.Parent [ player.Name ] |
At first I had no idea what the script was supposed to do, but then it occurred to me at work that it wasn't supposed to be mixed up in my Profile spawning script and was supposed to use its own script to save the data in it.