You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
Hi, I want to know how much data can be saved within each datastore and would it be a good idea to have multiple datastores. I used two datastores and I’m not sure if that’s necessary on that scale. -
What is the issue? Include screenshots / videos if possible!
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local replicatedstorage = game:GetService("ReplicatedStorage")
local dataservice = game:GetService("DataStoreService")
local datastore = dataservice:GetDataStore("DATASTORE")
local datalogin = dataservice:GetDataStore("Login")
local serverstorage = game:GetService("ServerStorage")
local AllPlayerData = serverstorage:WaitForChild("AllPlayerData")
local defaultstat = 0
local REWARD_CD = 86400
local function SavePlayerData(player)
local tries = 0
local success, errormessage
local playerfolder = player.Name
local data = {wins = player.leaderstats.Wins.Value,
kills = player.leaderstats.Kills.Value,
death = player.leaderstats.Death.Value,
coins = player.leaderstats.Coins.Value,
level = player.leaderstats.Level.Value,
inventory = {},
experience = player:GetAttribute("Experience") or 0,
totaltime = player:GetAttribute("TotalTime") or 0}
for i, swordtag in pairs(player:FindFirstChild(player.Name).Inventories:GetChildren()) do
table.insert(data.inventory, swordtag.Name)
end
repeat
success, errormessage = pcall(function()
datastore:SetAsync(player.UserId, data)
tries += 1
task.wait(1)
end)
until success or tries >= 3
success, errormessage = pcall(function()
datalogin:SetAsync(player.UserId, os.time())
end)
AllPlayerData:FindFirstChild(player.Name):Destroy()
if success == false then
warn(errormessage)
end
end
local function LoadPlayerData(player)
local tries = 0
local data, lastlogin
local success, errormessage
repeat success, errormessage = pcall(function()
data = datastore:GetAsync(player.UserId)
tries += 1
task.wait(1)
end)
until success or tries >= 3
success, errormessage = pcall(function()
lastlogin = datalogin:GetAsync(player.UserId)
end)
if success == false then
player:Kick("Unable to load data successfully")
warn(errormessage)
return
end
return data, lastlogin
end
game.Players.PlayerAdded:Connect(function(player)
local PlayerDataFolder = Instance.new("Folder")
PlayerDataFolder.Parent = AllPlayerData
PlayerDataFolder.Name = player.Name
local leaderboard = Instance.new("Folder")
leaderboard.Name = "leaderstats"
leaderboard.Parent = player
local PlayerInventory = Instance.new("Folder")
PlayerInventory.Name = "Inventories"
PlayerInventory.Parent = PlayerDataFolder
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderboard
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderboard
local death = Instance.new("IntValue")
death.Name = "Death"
death.Parent = leaderboard
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderboard
local coins = Instance.new("IntValue") --Just add Gui to display playerse gold amount
coins.Name = "Coins"
coins.Parent = leaderboard
local data, lastlogin = LoadPlayerData(player)
if data then
wins.Value = data.wins or defaultstat
kills.Value = data.kills or defaultstat
death.Value = data.death or defaultstat
coins.Value = data.coins or defaultstat
level.Value = data.level or defaultstat
player:SetAttribute("Experience", data.experience or 0)
else
--For when player joins the first time they wont have data
end
for i, folders in AllPlayerData:GetChildren() do
local folderclone = folders:Clone()
if folderclone.Name == player.Name then
folderclone.Parent = player
end
end
if data.inventory then
for i, swordtag in pairs(data.inventory) do
local SWORDTAG = Instance.new("BoolValue")
SWORDTAG.Name = swordtag
SWORDTAG.Parent = player:FindFirstChild(player.Name).Inventories
end
end
if lastlogin and data.totaltime then
local difftime = os.time() - lastlogin
local TotalTime = player:GetAttribute("TotalTime") or 0
player:SetAttribute("TotalTime", TotalTime + difftime)
else
player:Kick("PlayerTime didn't load successfully")
end
local ExperienceReq = 100 * (player.leaderstats.Level.Value/2 + 1)
player:SetAttribute("ExperienceRequirement", ExperienceReq)
player:GetAttributeChangedSignal("Experience"):Connect(function()
local PlayerExp = player:GetAttribute("Experience")
local ExperienceReq = 100 * (player.leaderstats.Level.Value/2 + 1)
player:SetAttribute("ExperienceRequirement", ExperienceReq)
if PlayerExp >= ExperienceReq then
player:SetAttribute("Experience", 0) --Current player exp set to 0 on every new level
player.leaderstats.Level.Value += 1
end
end)
player.leaderstats.Level:GetPropertyChangedSignal("Value"):Connect(function() --Level up VFX to play for every level up
local reward = 20 + (player.leaderstats.Level.Value * 5)
player.leaderstats.Coins.Value += reward
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
SavePlayerData(player)
end)
local function AutoSave()
while task.wait(300) do
for i, player in pairs(game.Players:GetPlayers()) do
SavePlayerData(player)
print("DATA AUTOSAVED")
end
end
end
coroutine.wrap(AutoSave)()
game:BindToClose(function() --Save everyones data is game shutdown
for i, player in pairs(game.Players:GetPlayers()) do
SavePlayerData(player)
end
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.