My DataStore Script Does not Work for 2 value, while it works if it`s saving only 1 value. Any Idea?
Asked by
5 years ago Edited 5 years ago
I have this script to save Wins and Cash of each player. This script works perfectly fine if it is only saving Win value. However, when I also try to save cash value, it resets everything to 0 instead of saving current value. Can someone please help me?
---------- EDIT-----------
After I posted this question, I tried to debug my self by printing. The problem I found is that no matter what I do, the script wont correctly read the value of IntValue. From the commandbar, it prints the correct value but when this script runs it returns 0.
01 | local dataStore = game:GetService( "DataStoreService" ) |
02 | local playerData = dataStore:GetDataStore( "playerData" ) |
05 | game.Players.PlayerAdded:Connect( function (player) |
06 | local leaderStats = Instance.new( "Folder" ) |
07 | leaderStats.Name = "leaderstats" |
08 | leaderStats.Parent = player |
10 | local win = Instance.new( "IntValue" ) |
12 | win.Parent = leaderStats |
15 | local otherData = Instance.new( "Folder" ) |
16 | otherData.Name = "otherData" |
17 | otherData.Parent = player |
19 | local cash = Instance.new( "IntValue" ) |
21 | cash.Parent = otherData |
24 | local success, errorMessage = pcall ( function () |
25 | data = playerData:GetAsync(player.UserId.. "-playerData" ) |
31 | print ( "successfully obtained Data" ) |
32 | print (data [ 1 ] .. " " .. data [ 2 ] ) |
35 | elseif errorMessage then |
36 | print ( "Unseccessfull DataStore" ) |
41 | game.Players.PlayerRemoving:Connect( function (player) |
42 | local wins = player.leaderstats.Wins.Value |
43 | local cash = player.otherData.Cash.Value |
44 | local datas = { wins, cash } |
45 | local success, errorMessage = pcall ( function () |
46 | playerData:SetAsync(player.UserId.. "-playerData" ,datas ) |
47 | print (datas [ 1 ] .. " " .. datas [ 2 ] ) |
51 | print ( "Successed on Saving Data" ) |
53 | elseif errorMessage then |
54 | print ( "Unsuccessful DataSave" ) |