So, I've been looking for answers all over for days on end. I haven't been able to find anything new for a while now, so I think asking a question here is my last option.
I've changed values around, I've changed the structure of the code, I've changed how the script accesses the values. But with no success. It did work at one point, but I needed to change it and now I can't remember for the life of me what I did.
If anyone can help, it would be greatly appreciated, here is the code:
local dataStoreService = game:GetService("DataStoreService")
local StatStore = dataStoreService:GetDataStore("StatStore")
game.Players.PlayerAdded:Connect(function(player)
game.Workspace:WaitForChild(player.Name)
player:WaitForChild("leaderstats")
local Key = player.UserId
local Leaderstats = player.leaderstats
local Steps = Leaderstats.Steps
local SpeedBoost = player.PlayerGui.Codes.SpeedBoost
local DefaultData = {
["Steps"] = 0,
["SpeedBoost"] = false
}
local Stats
local recieved, notRecieved = pcall(function()
Stats = StatStore:GetAsync(Key) or DefaultData
end)
if recieved then
Steps.Value = Stats
SpeedBoost.Value = Stats
print("Stats were recieved")
else
StatStore:SetAsync(Key, DefaultData)
print("Stats were not recieved")
warn(notRecieved)
end
wait(0.1)
local Stats = {
["Steps"] = Leaderstats.Steps.Value,
["SpeedBoost"] = player.PlayerGui.Codes.SpeedBoost.Value
}
while wait(300) do
local saved, notSaved = pcall(function()
StatStore:UpdateAsync(player.UserId, function(oldValue)
print("Stats were auto saved!")
return Stats
end)
end)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local User = game.Players:FindFirstChild(player.Name)
local Leaderstats = User.leaderstats
local NewSteps = script.Parent.StepLoop.Steps
local Steps = Leaderstats.Steps
local SpeedBoost = User.PlayerGui.Codes.SpeedBoost
local DefaultData = {
["Steps"] = 0,
["SpeedBoost"] = false
}
local Key = User.UserId
local Stats = {
["Steps"] = Leaderstats.Steps.Value,
["SpeedBoost"] = User.PlayerGui.Codes.SpeedBoost.Value
}
local success, result = pcall(function()
StatStore:UpdateAsync(Key, function(oldValue)
return Stats
end)
end)
if success then
print("Stats were saved")
else
print("Stats were not saved")
warn(result)
end
end)
(SpeedBoost is a Bool value and Steps is an Int value)
