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.
local dataStore = game:GetService("DataStoreService")
local playerData = dataStore:GetDataStore("playerData")
game.Players.PlayerAdded:Connect(function(player)
local leaderStats = Instance.new("Folder")
leaderStats.Name = "leaderstats"
leaderStats.Parent = player
local win = Instance.new("IntValue")
win.Name = "Wins"
win.Parent = leaderStats
local otherData = Instance.new("Folder")
otherData.Name = "otherData"
otherData.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = otherData
local data = nil
local success, errorMessage = pcall(function()
data = playerData:GetAsync(player.UserId.."-playerData")
end)
if success then
if data then
win.Value = data[1]
cash.Value = data[2]
print("successfully obtained Data")
print(data[1].." ".. data[2])
end
elseif errorMessage then
print("Unseccessfull DataStore")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local wins = player.leaderstats.Wins.Value
local cash = player.otherData.Cash.Value
local datas = {wins, cash}
local success, errorMessage = pcall(function()
playerData:SetAsync(player.UserId.."-playerData",datas )
print(datas[1].." ".. datas[2])
end)
if success then
print("Successed on Saving Data")
elseif errorMessage then
print("Unsuccessful DataSave")
warn(errorMessage)
end
end)

4 comments
Thanks for your Help :D However, it still seems to reset all my data.
If it all gets reset but doesn't return nil that means the script isn't able to get the values or when you try to update the values you are trying to update them as a client not server which in that case they don't really change but give the player the apperance they have
I dont know the reason why but the script started to work after i republished into different place.
You may have had something else there that you didn't notice that changed them or its just roblox being weird and doing that