Okay, so I scripted this data store and it works in my other games but doesn't for this game. I have checked this many times and it doesn't work, I'm very confused on why it doesn't save. Ignore the daily reward, what I'm focusing on is the data store.
Heres the script.
local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")
local hourWait = 24
local possibleRewards = {50,50,50,50,50,50,50,50,50,70,63,63,63,63,63,70,70,70,90,90,100}
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Parent = leaderstats
local timeNow = os.time()
local data
pcall(function()
data = DataStore:GetAsync(player.UserId.."-dailyReward")
print("Getting data")
end)
if data ~= nil then
-- Returning player to the game.
local timeSinceLastClaim = timeNow - data -- Number in sec since last reward was claimed
print("Time since last claim"..timeSinceLastClaim)
if (timeSinceLastClaim / 3600) >= hourWait then
-- They are elgible for a reward! :D
local reward = possibleRewards[math.random(1,#possibleRewards)]
game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
local connection
connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
print("Reward Claimed!")
player.leaderstats.Money.Value = player.leaderstats.Money.Value + reward
DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
connection:Disconnect()
end
end)
else
print("Player is uneligible right now. :(")
end
else
-- New Player
print("New player!")
local reward = possibleRewards[math.random(1,#possibleRewards)]
game.ReplicatedStorage.ShowDailyReward:FireClient(player,hourWait,reward)
local connection
connection = game.ReplicatedStorage.ClaimReward.OnServerEvent:Connect(function(triggeringPlayer)
if triggeringPlayer == player then
print("Reward Claimed!")
player.leaderstats.Money.Value = player.leaderstats.Money.Value + reward
DataStore:SetAsync(player.UserId.."-dailyReward",os.time())
connection:Disconnect()
end
end)
end
game.Players.PlayerRemoving:Connect(function(player)
local data
pcall(function()
data = DataStore:GetAsync(player.UserId.."-dailyReward")
print("Getting the Data... or is it?")
end)
end)
--[[if data == nil then
-- New player once again.
pcall(function()
local timeVal = os.time()
DataStore:SetAsync(player.UserId.."-dailyReward")
end)
print("Saved because you are a new player")
end--]]
local dataFolder = Instance.new("Folder")
dataFolder.Name = player.Name
dataFolder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = dataFolder
local MoneyData
local success,errormessage = pcall(function()
MoneyData = DataStore:GetAsync("Money-"..player.UserId)
end)
if success then
print("data saved")
if MoneyData then
Money.Value = MoneyData
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync("Money-"..player.UserId,player.leaderstats.Money.Value)
end)
end)

1 comment
Instead of
local leaderstats = Instance.new("Folder")thenleaderstats.Parent = player, just dolocal leaderstats = Instance.new("Folder, player")