Answered by
6 years ago Edited 6 years ago
You should save using tables. This will make it easier in the future to add other values too. I fixed up your code below but keep in mind this is untested.
01 | local Players = game:GetService( "Players" ) |
02 | local DataStoreService = game:GetService( "DataStoreService" ) |
03 | local DataStore = DataStoreService:GetDataStore( "-xBuckDatax-" ) |
06 | local function Save(player) |
07 | local key = "Player_" ..player.UserId |
10 | [ "ObbyBucks" ] = player.leaderstats [ "ObbyBucks<|>" ] .Value |
14 | local success, err = pcall ( function () |
15 | DataStore:SetAsync(key, save) |
19 | warn( "Failed to over-write data" .. tostring (err)) |
25 | local function Load(player) |
27 | local key = player.UserId |
29 | local folder = Instance.new( "Folder" ) |
30 | folder.Name = "leaderstats" |
31 | folder.Parent = player |
33 | local money = Instance.new( "IntValue" ) |
34 | money.Name = "ObbyBucks<|>" |
39 | local success, err = pcall ( function () |
40 | moneyAlready.Value = DataStore:GetAsync(key) |
44 | warn( "Failed to read data" .. tostring (err)) |
49 | money.Value = moneyAlready.ObbyBucks |
55 | Players.PlayerAdded:Connect(Load) |
56 | Players.PlayerRemoving:Connect(Save) |
All I did was improve the code.
What I did:
Split the DataStore
variables, made the saving and loading in a function that you control when it's called, editing the save and load system, removed deprecated code.
More information on DataStores