I used this script and i dont know why dont work to save when "player left and enter again"
local AutoSaveInterval = 15
local datastoreService = game:GetService("DataStoreService")
local experiencestore = datastoreService:GetDataStore("PlayerExperience")
local dataStore = datastoreService:GetDataStore("DailyRewards")
local HourWait = 24
local sessionData = {}
local possibleRewards = {5,5,5,5,5,5,5,5,5,5,5,5,5,10,10,10,10,10,10,10,15,15,15,15,15,500,500}
local myDataStore = datastoreService:GetDataStore("myDataStore")
local playerData = datastoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderStats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Emeralds"
cash.Parent = leaderstats
local time = os.time()
local data
pcall(function()
data = dataStore:GetAsync(player.UserId.."-DailyReward")
print("GettingData")
end)
if data ~= nil then
--Returning player to game
local timesinceLastClaim = timenow - data
print("time since last claim"..timesinceLastClaim)
if (timesinceLastClaim / 3600) >= HourWait then
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.Emeralds.Value = player.leaderStats.Emeralds.Value + reward
dataStore:SetAsync(player.UserId.."-DailyLogin",os.time())
connection:Disconnected()
end
end)
else
print("player is unegible 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.Emeralds.Value = player.leaderStats.Emeralds.Value + reward
dataStore:SetAsync(player.UserId.."-DailyLogin",os.time())
connection:Disconnected()
end
end)
end
local data
local sucess, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-Cash")
end)
if sucess then
cash.Value = data
else
print("CANT GET MONEY")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data
pcall(function()
data = dataStore:GetAsync(player.UserId.."-DailyLogin")
print("GettingData")
end)
if data == nil then
local timeValue = os.time()
dataStore:SetAsync(player.UserId.."-DailyLogin")
end
print("Saved Because Your a New Player")
local sucess, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash",player.leaderStats.Emeralds.Value)
end)
if sucess then
print("Data sucefully saved")
else
print("there's an error saving data")
warn(errormessage)
end
end)
