I was testing my game (data worked) then I quit the test, tested it again a couple minutes later, and it stopped saving. The code was working but now it does not! There are no errors in the output!
Here is my code:
local players = game:GetService("Players")
--local dataStoreService = game:GetService()
local replicatedStorage = game:GetService("ReplicatedStorage")
local tools = replicatedStorage:WaitForChild("Tools")
local dataStoreService = game:GetService("DataStoreService")
local playerDataStore = dataStoreService:GetDataStore("LittleData")
-----------Hourly Reward-----------
local hourlyCashRewards = {
["100"] = 75,
["1000"] = 15,
["10000"] = 5,
["1000000"] = 5,
}
local function getreward()
local randomNumber = math.random(1, 100)
local counter = 0
for reward, weight in pairs(hourlyCashRewards) do
counter += weight
if randomNumber <= counter then
return tonumber(reward)
end
end
end
local hourWait = 1
local rewardDataStore = dataStoreService:GetDataStore("HourlyReward")
----------------------
local function onCharacterAdded(character)
tools:WaitForChild("Weight"):Clone().Parent = game.Players:GetPlayerFromCharacter(character):WaitForChild("Backpack")
character:WaitForChild("Humanoid").Died:Connect(function()
--game.Players:GetPlayerFromCharacter(character):Kick("Error 1: Something went wrong.")
end)
end
local function getDataKey(player)
return player.UserId.."_Data"
end
local function onPlayerAdded(player)
--leaderstats (visible stats)
local petsEquipped = Instance.new("Folder")
petsEquipped.Name = player.Name
petsEquipped.Parent = workspace.PlayerPets
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local strength = Instance.new("IntValue")
strength.Name = "Strength"
strength.Parent = leaderstats
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 0
cash.Parent = leaderstats
--information about the player stats
local playerInfo = Instance.new("Folder")
playerInfo.Name = "PlayerInfo"
playerInfo.Parent = player
local maxStrength = Instance.new("IntValue")
maxStrength.Name = "MaxStrength"
maxStrength.Value = 10
maxStrength.Parent = playerInfo
local maxSrengthPrice = Instance.new("IntValue")
maxSrengthPrice.Value = 100
maxSrengthPrice.Name = "UpgradeMaxStrength"
maxSrengthPrice.Parent = playerInfo
local StrengthPerClick = Instance.new("IntValue")
StrengthPerClick.Value = 1
StrengthPerClick.Name = "StrengthPerClick"
StrengthPerClick.Parent = playerInfo
local strengthPrice = Instance.new("IntValue")
strengthPrice.Name = "UpgradeStrength"
strengthPrice.Value = 100
strengthPrice.Parent = playerInfo
local isMining = Instance.new("BoolValue")
isMining.Value = false
isMining.Name = "IsMining"
isMining.Parent = playerInfo
local data
local key = getDataKey(player)
local success, errorMessage = pcall(function()
data = playerDataStore:GetAsync(key)
end)
if data and success then
--get cash
cash.Value = data.cash
--get strength
strength.Value = data.strength
--get inventory
for _, petName in pairs(data.inventory) do
for _, pet in pairs(game.ServerStorage.Pets:GetChildren()) do
if pet.Name == petName then
pet:Clone().Parent = pets
end
end
end
--get player info
for i, v in pairs(data.playerInfo) do
local splitInfo = string.split(v, "-")
local name = splitInfo[1]
local value = splitInfo[2]
local foundValue = player.PlayerInfo:FindFirstChild(name)
if foundValue then
if foundValue:IsA("StringValue") then
foundValue.Value = tostring(value)
elseif foundValue:IsA("IntValue") or foundValue:IsA("NumberValue") then
foundValue.Value = tonumber(value)
end
else
local newValue = Instance.new("StringValue", player.PlayerInfo)
newValue.Name = name
newValue.Value = value
end
end
else
warn("Error:" , errorMessage)
end
game.ServerStorage.Pets:Clone().Parent = game.ReplicatedStorage
for _, pet in pairs(game.ReplicatedStorage.Pets:GetChildren()) do
game.ReplicatedStorage.Remotes.PetDataBase:FireClient(player, pet)
end
--Hourly Reward
local timeNow = os.time()
local data
local key = player.UserId .. "-reward"
local success, errorMessage = pcall(function()
data = rewardDataStore:GetAsync(key)
end)
if success and data then
local timePassed = timeNow - data
if (timePassed/3600) >= hourWait then
--they can claim a reward
local reward = getreward()
print("The reward for " .. player.Name .. " is" , reward)
if reward then
player.leaderstats.Cash.Value += reward
local formattedMessage = "Thanks for playing! You have earned $" .. tostring(reward) .. "! Come back in 1 hour."
game.ReplicatedStorage.Remotes.HourlyReward:FireClient(player, formattedMessage)
local connection
connection = game.ReplicatedStorage.Remotes.HourlyReward.OnServerEvent:Connect(function(playerTriggered)
if player == playerTriggered then
print("Reward claimed!")
rewardDataStore:SetAsync(key, os.time())
connection:Disconnect()
end
end)
end
else
print(timePassed, "is not long enough!", player.Name)
end
else
print("New player!")
--new player
--they can claim a reward
local reward = getreward()
print("The reward for " .. player.Name .. " is" , reward)
if reward then
player.leaderstats.Cash.Value += reward
local formattedMessage = "Thanks for playing! You have earned $" .. tostring(reward) .. "! Come back in 1 hour."
game.ReplicatedStorage.Remotes.HourlyReward:FireClient(player, formattedMessage)
local connection
connection = game.ReplicatedStorage.Remotes.HourlyReward.OnServerEvent:Connect(function(playerTriggered)
if player == playerTriggered then
print("Reward claimed!")
rewardDataStore:SetAsync(key, os.time())
connection:Disconnect()
end
end)
end
end
-----------------
player.CharacterAdded:Connect(onCharacterAdded)
end
game.Players.PlayerRemoving:Connect(function(player)
local data = {}
--save cash
data.cash = player.leaderstats.Cash.Value
--save strength
data.strength = player.leaderstats.Strength.Value
--save pets
data.inventory = {}
for index, pet in pairs(player.Pets:GetChildren()) do
table.insert(data.inventory, pet.Name)
end
for index, pet in pairs(workspace.PlayerPets:FindFirstChild(player.Name):GetChildren()) do
if pet:IsA("Model") and pet:FindFirstChild("Configuration") then
table.insert(data.inventory, pet.Name)
end
end
--save player info
data.playerInfo = {}
for _, value in pairs(player.PlayerInfo:GetChildren()) do
local together = value.Name .. "-" .. tostring(value.Value)
table.insert(data.playerInfo, together)
end
local key = getDataKey(player)
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(key, data)
end)
if success then
warn("Data: Saved data for player (" .. player.Name ..")" )
else
warn("Error:" , errorMessage)
end
end)
game:BindToClose(function()
wait(5)
end)
players.PlayerAdded:Connect(onPlayerAdded)