the total time. I have the datastore already. It locates in the ServerScriptService and this taskscript locates in the StarterGui
Here is the saving system:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local database = DataStoreService:GetDataStore("(Mydatastore)")
local sessionData = {}
function PlayerAdded(player)
...
local tasks = Instance.new(Folder)
tasks.Name = "tasks"
local hraha = Instance.new("IntValue", tasks)
hraha.Name = "hraha"
local hraha2 = Instance.new("IntValue", tasks)
hraha2.Name = "hraha2"
local hraha3 = Instance.new("IntValue", tasks)
hraha3.Name = "hraha3"
local ht1 = Instance.new("IntValue", tasks)
ht1.Name = "ht1"
local ht2 = Instance.new("IntValue", tasks)
ht2.Name = "ht2"
local ht3 = Instance.new("IntValue", tasks)
ht3.Name = "ht3"
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
print("toimii")
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
loaded.Value = 1
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default data")
playerData = {
...
["r2aha1"] = 0,
["r3aha1"] = 0,
["hr1aha"] = 0,
["hr2aha"] = 0,
["hr3aha"] = 0,
["raha2"] = 0,
["raha3"] = 0,
["raha4"] = 0,
["et22"] = 0,
["et33"] = 0,
["mt1"] = 0,
["mt2"] = 0,
["mt3"] = 0,
["ht1"] = 0,
["ht2"] = 0,
["ht3"] = 0,
["it1"] = 0,
["it2"] = 0,
["it3"] = 0,
...
}
end
sessionData[player.UserId] = playerData
else
warn("Unable to get data for", player.UserId)
player:Kick("Unable to load your data. Try again later")
end
hraha.Value = sessionData[player.UserId]["hr1aha"]
hraha2.Value = sessionData[player.UserId]["hr2aha"]
hraha3.Value = sessionData[player.UserId]["hr3aha"]
ht1.Value = sessionData[player.UserId]["ht1"]
ht2.Value = sessionData[player.UserId]["ht2"]
ht3.Value = sessionData[player.UserId]["ht3"]
hraha.Changed:Connect(function()
sessionData[player.UserId].hr1aha = hraha.Value
print("hraha1")
sessionData[player.UserId].ht1 = ht1.Value
print("ht1")
end)
hraha2.Changed:Connect(function()
sessionData[player.UserId].hr2aha = hraha2.Value
print("hraha2")
sessionData[player.UserId].ht2 = ht2.Value
print("ht2")
end)
hraha3.Changed:Connect(function()
sessionData[player.UserId].hr3aha = hraha3.Value
print("hraha3")
sessionData[player.UserId].ht3 = ht3.Value
print("ht3")
end)
tasks.Parent = player
end
Players.PlayerAdded:Connect(PlayerAdded)
function PlayerLeaving(player)
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for", player.Name)
else
warn("Unable to save for", player.Name)
end
end
end
Players.PlayerRemoving:Connect(PlayerLeaving)
function ServerShutdown()
if RunService:IsStudio() then
return
end
print("Server shutting down...")
for _, player in pairs(Players:GetPlayers()) do
task.spawn(function()
PlayerLeaving(player)
end)
end
end
game:BindToClose(ServerShutdown)
(hraha2 is the same as hr2)
there are also the other tasks values but only the hr2 and ht2 is the value that we want to fix.
Also there is another server script that tells the datastore script that ht2 and hr2 value has changed.
Here it is:
game.ReplicatedStorage.tasks.hr.OnServerEvent:Connect(function(player, newCoins)
local saunat = player:WaitForChild("tasks")
local sauna2 = saunat:FindFirstChild("hraha")
wait(.5)
sauna2.Value = newCoins
end)
game.ReplicatedStorage.tasks.hr2.OnServerEvent:Connect(function(player, newCoins)
local saunat = player:WaitForChild("tasks")
local sauna2 = saunat:FindFirstChild("hraha2")
wait(.5)
sauna2.Value = newCoins
end)
game.ReplicatedStorage.tasks.hr3.OnServerEvent:Connect(function(player, newCoins)
local saunat = player:WaitForChild("tasks")
local sauna2 = saunat:FindFirstChild("hraha3")
wait(.5)
sauna2.Value = newCoins
end)
game.ReplicatedStorage.tasks.h1.OnServerEvent:Connect(function(player, newCoins)
local saunat = player:WaitForChild("tasks")
local sauna2 = saunat:FindFirstChild("ht1")
wait(.5)
sauna2.Value = newCoins
end)
game.ReplicatedStorage.tasks.h2.OnServerEvent:Connect(function(player, newCoins)
local saunat = player:WaitForChild("tasks")
local sauna2 = saunat:FindFirstChild("ht2")
wait(.5)
sauna2.Value = newCoins
end)
game.ReplicatedStorage.tasks.h3.OnServerEvent:Connect(function(player, newCoins)
local saunat = player:WaitForChild("tasks")
local sauna2 = saunat:FindFirstChild("ht3")
wait(.5)
sauna2.Value = newCoins
end)
Hopefully this helps…