Hi, I have a data store script that gets data and saves data. But the saving part doesn't work. I've tested it by changed a player's cash value and added print functions on getting the data and saving the data. The print function on getting the data works but nothing for saving the data. No errors or etc was warned to the output. This is the script:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("ServerDataStore")
local discordModule = require(game.ServerStorage.DiscordModule)
game.Players.PlayerAdded:Connect(function(player)
local successValue = false
for i = 1,3 do
local success,data = pcall(function()
return myDataStore:GetAsync(player.UserId)
end)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue",leaderstats)
cash.Name = "Cash"
local xp = Instance.new("IntValue",leaderstats)
xp.Name = "Exp"
if success then
successValue = true
if data then
local table2 = data
cash.Value = table2[1]
xp.Value = table2[2]
break
else
cash.Value = 0
xp.Value = 0
break
end
else
warn("Data could not be retrieved! Retrying...")
end
end
if not successValue then
discordModule.sendMessage("Player_"..player.UserId.."'s data could not be retrieved!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local successValue = false
for i = 1,3 do
local myTable = {}
table.insert(myTable,1,player.leaderstats.Cash.Value)
table.insert(myTable,2,player.leaderstats.Exp.Value)
local success,errormessage = pcall(function()
myDataStore:SetAsync(player.UserId,myTable)
end)
if not success then
warn("Data could not be saved! Retrying... Error:",errormessage)
return
elseif success then
successValue = true
break
end
end
if not successValue then
discordModule.sendMessage("Player_"..player.UserId.."'s data could not be saved!")
end
end)
Please Help!
