- What do you want to achieve? Keep it simple and clear!
i want to store players data using data store service
- What is the issue? Include screenshots / videos if possible!
for no reason it saves a part of the data and the other part not , how ? well my data is kinda big , it has 2 tables in the main data table that include the player daily and weekly quests and the data store does not save all the quests !
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i tried debugging it using prints and it prints the data correctly so the data has all the arguments and all quests without any problems but when loading the data after it saves (i am sure that it saves the data) some quests are missing !
here is the code :-
local runservice = game:GetService("RunService")
local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local datastoresurvice = game:GetService("DataStoreService")
local database = datastoresurvice:GetDataStore("values")
local ServerModuleScripts = ServerStorage:WaitForChild("ModuleScripts")
local RewardsManager = require(ServerModuleScripts:WaitForChild("RewardsManager"))
local ReplicatedModuleScripts = ReplicatedStorage:WaitForChild("ModuleScripts")
local DailyQuests = require(ReplicatedModuleScripts:WaitForChild("DailyQuests"))
local WeeklyQuests = require(ReplicatedModuleScripts:WaitForChild("WeeklyQuests"))
dates = {}
function saveplayerdata(player:Player)
local times = 0
local sucsess,errormsg = nil , nil
local PlayerDailyQuests = if player:FindFirstChild("DailyQuests") then player.DailyQuests:GetChildren() else RewardsManager.GiveDailyQuests(player)
local PlayerWeeklyQuests = if player:FindFirstChild("WeeklyQuests") then player.WeeklyQuests:GetChildren() else RewardsManager.GiveWeeklyQuests(player)
local Trails = {}
if player:FindFirstChild("Trails") then
for i , Trail in pairs(player.Trails:GetChildren()) do
table.insert(Trails , Trail.Name)
end
end
local DailyQuestsTable = {}
for i , Value in pairs(PlayerDailyQuests) do
local QuestIndex = nil
for j , Quest in DailyQuests do
if Quest == Value.Name then
QuestIndex = j
break
end
end
DailyQuestsTable[QuestIndex] = Value.Value
end
local WeeklyQuestsTable = {}
for i , Value in pairs(PlayerWeeklyQuests) do
local QuestIndex = nil
for j , Quest in WeeklyQuests do
if Quest == Value.Name then
QuestIndex = j
break
end
end
WeeklyQuestsTable[QuestIndex] = Value.Value
end
local PowerUpsTable = {}
if player:FindFirstChild("PowerUps") then
for i , Value in pairs(player.PowerUps:GetChildren()) do
table.insert(PowerUpsTable , Value.Name)
table.insert(PowerUpsTable , Value.Value)
end
end
local PlayerData = player:FindFirstChild("Data") or player
local Strike = PlayerData:FindFirstChild("Strike")
local Data = {
["Cash"] = dates[player.UserId].Cash or 0,
["Trails"] = Trails,
["UsedTrail"] = dates[player.UserId].UsedTrail or "",
["LastJoin"] = dates[player.UserId].LastJoin or os.time(),
["Strike"] = if Strike then Strike.Value else 1,
["DailyQuests"] = DailyQuestsTable,
["LastWeekJoin"] = dates[player.UserId].LastWeekJoin or os.time(),
["WeeklyQuests"] = WeeklyQuestsTable,
["MaxPowerUps"] = dates[player.UserId].MaxPowerUps or 4,
["PowerUps"] = PowerUpsTable,
["Level"] = dates[player.UserId].Level or 1,
["GamesForLevel"] = dates[player.UserId].GamesForLevel or 0,
}
while not sucsess and times < 10 do
sucsess,errormsg = pcall(function()
database:SetAsync(tostring(player.UserId) , Data)
end)
if not sucsess then
warn(tostring(errormsg))
end
times += 1
task.wait(0.01)
end
if dates[player.UserId] then
dates[player.UserId] = nil
end
end
function loadplayerdata(player:Player)
local DailyQuestsFolder = Instance.new("Folder")
DailyQuestsFolder.Name = "DailyQuests"
DailyQuestsFolder.Parent = player
local WeeklyQuestsFolder = Instance.new("Folder")
WeeklyQuestsFolder.Name = "WeeklyQuests"
WeeklyQuestsFolder.Parent = player
local PowerUpsFolder = Instance.new("Folder")
PowerUpsFolder.Name = "PowerUps"
PowerUpsFolder.Parent = player
local fol = Instance.new("Folder")
fol.Parent = player
fol.Name = "Data"
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = fol
local UsedTrail = Instance.new("StringValue")
UsedTrail.Name = "UsedTrail"
UsedTrail.Parent = fol
local LastJoin = Instance.new("IntValue")
LastJoin.Name = "LastJoin"
LastJoin.Parent = fol
local Strike = Instance.new("IntValue")
Strike.Name = "Strike"
Strike.Parent = fol
local LastWeekJoin = Instance.new("IntValue")
LastWeekJoin.Name = "LastWeekJoin"
LastWeekJoin.Parent = fol
local MaxPowerUps = Instance.new("IntValue")
MaxPowerUps.Name = "PowerUpsStorage"
MaxPowerUps.Parent = fol
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = fol
local GamesForLevel = Instance.new("IntValue")
GamesForLevel.Name = "GamesForLevel"
GamesForLevel.Parent = fol
local sucsess , playerdata = nil
local times = 0
local CashValue = 0
local UsedTrailValue = ""
local LastJoinValue = os.time()
local LastWeekJoinValue = os.time()
local StrikeValue = 1
local PlayerDailyQuests = nil
local PlayerWeeklyQuests = nil
local MaxPowerUpsValue = 4
local LevelValue = 1
local GamesForLevelValue = 0
local HasDailyReward = false
local DefaultData = {
["Cash"] = 0,
["Trails"] = {},
["UsedTrail"] = "",
["LastJoin"] = os.time(),
["Strike"] = 1,
["DailyQuests"] = RewardsManager.GiveDailyQuests(player),
["LastWeekJoin"] = os.time(),
["WeeklyQuests"] = RewardsManager.GiveWeeklyQuests(player),
["MaxPowerUps"] = 4,
["PowerUps"] = {},
["Level"] = 1,
["GamesforLevel"] = 0,
}
repeat
sucsess , playerdata = pcall(function()
return database:GetAsync(tostring(player.UserId))
end)
if sucsess and playerdata then
CashValue = playerdata.Cash or 0
UsedTrailValue = playerdata.UsedTrail or ""
LastJoinValue = playerdata.LastJoin or os.time()
StrikeValue = playerdata.Strike or 1
PlayerDailyQuests = playerdata.DailyQuests or nil
PlayerWeeklyQuests = playerdata.WeeklyQuests or nil
LastWeekJoinValue = playerdata.LastWeekJoin or os.time()
MaxPowerUpsValue = playerdata.MaxPowerUps or 4
LevelValue = playerdata.Level or 1
GamesForLevelValue = playerdata.GamesForLevel or 0
if (os.time() - LastJoinValue) >= 86400 then
LastJoinValue = os.time()
if StrikeValue < 7 then
StrikeValue += 1
else
StrikeValue = 1
end
Strike.Value = StrikeValue
PlayerDailyQuests = RewardsManager.GiveDailyQuests(player)
HasDailyReward = true
end
if (os.time() - LastWeekJoinValue) >= 604800 then
LastWeekJoinValue = os.time()
PlayerWeeklyQuests = RewardsManager.GiveWeeklyQuests(player)
end
local ModifiedData = {
["Cash"] = CashValue,
["Trails"] = playerdata.Trails or {},
["UsedTrail"] = UsedTrailValue,
["LastJoin"] = LastJoinValue,
["Strike"] = StrikeValue,
["DailyQuests"] = PlayerDailyQuests or RewardsManager.GiveDailyQuests(player),
["LastWeekJoin"] = LastWeekJoinValue,
["WeeklyQuests"] = PlayerWeeklyQuests or RewardsManager.GiveWeeklyQuests(player),
["MaxPowerUps"] = MaxPowerUpsValue,
["PowerUps"] = playerdata.PowerUps or {},
["Level"] = LevelValue,
["GamesForLevel"] = GamesForLevelValue,
}
dates[player.UserId] = ModifiedData
else
dates[player.UserId] = DefaultData
warn(tostring(playerdata))
end
times += 1
until sucsess or times > 10
Cash.Value = CashValue
UsedTrail.Value = UsedTrailValue
LastJoin.Value = LastJoinValue
Strike.Value = StrikeValue
LastWeekJoin.Value = LastWeekJoinValue
MaxPowerUps.Value = MaxPowerUpsValue
Level.Value = LevelValue
GamesForLevel.Value = GamesForLevelValue
Cash.Changed:Connect(function()
local Addition = Cash.Value - dates[player.UserId].Cash
if Addition > 0 then
local Succuss2 , SubsicriptionInfo =pcall(function()
MarketPlaceService:GetUserSubscriptionStatusAsync(player , "EXP-7087747329701970194")
end)
if SubsicriptionInfo and SubsicriptionInfo.IsSubscribed == true then
Cash.Value += Addition
end
end
dates[player.UserId].Cash = Cash.Value
end)
UsedTrail.Changed:Connect(function()
dates[player.UserId].UsedTrail = UsedTrail.Value
end)
MaxPowerUps.Changed:Connect(function()
dates[player.UserId].MaxPowerUps = MaxPowerUps.Value
end)
Level.Changed:Connect(function()
dates[player.UserId].Level = Level.Value
end)
GamesForLevel.Changed:Connect(function()
dates[player.UserId].GamesForLevel = GamesForLevel.Value
end)
local TrailsFolder = Instance.new("Folder")
TrailsFolder.Name = "Trails"
TrailsFolder.Parent = player
for i , Trail in pairs(dates[player.UserId].Trails) do
local Value = Instance.new("IntValue")
Value.Name = Trail
Value.Parent = TrailsFolder
end
for i , Quest in pairs(dates[player.UserId].DailyQuests) do
local Value = Instance.new("StringValue")
Value.Name = DailyQuests[tonumber(i)]
Value.Value = tostring(Quest)
Value.Parent = DailyQuestsFolder
end
for i , Quest in pairs(dates[player.UserId].WeeklyQuests) do
local Value = Instance.new("StringValue")
Value.Name = WeeklyQuests[tonumber(i)]
Value.Value = tostring(Quest)
Value.Parent = WeeklyQuestsFolder
end
for i , PowerUp in pairs(dates[player.UserId].PowerUps) do
if type(PowerUp) == "number" then
continue
end
local Value = Instance.new("IntValue")
Value.Name = PowerUp
Value.Value = dates[player.UserId].PowerUps[i + 1]
Value.Parent = PowerUpsFolder
end
times = 0
local Success2 , ErrorMessage = nil
local ModifiedData = playerdata or DefaultData
ModifiedData.LastJoin = LastJoinValue
ModifiedData.Strike = StrikeValue
ModifiedData.LastWeekJoin = LastWeekJoinValue
repeat
Success2 , ErrorMessage = pcall(function()
database:SetAsync(tostring(player.UserId) , ModifiedData)
end)
if not Success2 then
warn(tostring(ErrorMessage))
end
times += 1
until Success2 or times > 10
if HasDailyReward == true then
RewardsManager.PlayerDailyRewards(player)
end
end
game.Players.PlayerAdded:Connect(loadplayerdata)
game.Players.PlayerRemoving:Connect(saveplayerdata)
game:BindToClose(function()
if runservice:IsStudio() then
task.wait(5)
else
task.wait(30)
end
end)