So the game I am making is going to have 30+ players in a lobby. The datastore I currently have is datastore2 and it makes instances and saves armour in a string using JSONEncode. I realised is the best way to do it, as in will this be more for the server to maintain, or is it best to use table?
My current script;
local HttpService = game:GetService("HttpService")
local players = game:GetService("Players")
local Datastore2 = require(1936396537) -- Get datastore2 module
Datastore2.Combine("Inventory", "Gold", "Armour", "Potions", "Weapons","Health","Damage","MagicDamage","Level", "Experience","MaxExperience" )
local function addPlayerData(player)
--// Creates a folder to hold the values and subfolders
local dataSaveFolder = Instance.new("Folder")
dataSaveFolder.Name = player.Name.."_Data"
dataSaveFolder.Parent = workspace.Userdata
--// Inventory System
local Inventory = Instance.new("Folder", dataSaveFolder); Inventory.Name = "Inventory"
local playerGold = Instance.new("IntValue", Inventory); playerGold.Name = "Gold"
local playerArmour = Instance.new("StringValue", Inventory); playerArmour.Name = "Armour"
local playerPotions = Instance.new("StringValue", Inventory); playerPotions.Name = "Potions"
local playerWeapons = Instance.new("StringValue", Inventory); playerWeapons.Name = "Weapons"
--// Player Stats
local playerHealth = Instance.new("IntValue", Inventory); playerHealth.Name = "Health"
local playerDamage = Instance.new("IntValue", Inventory); playerDamage.Name = "Damage"
local playerMagicDmg = Instance.new("IntValue", Inventory); playerMagicDmg.Name = "MagicDamage"
--// Players Level
local LevelSystem = Instance.new("Folder", dataSaveFolder); LevelSystem.Name = "LevelSystem"
local playerLevel = Instance.new("IntValue", LevelSystem); playerLevel.Name = "Level"
local playerXp = Instance.new("IntValue", LevelSystem); playerXp.Name = "Experience"
local playerMaxXp = Instance.new("IntValue", LevelSystem); playerMaxXp.Name = "MaxExperience"
--// Gets DataStores and initial values
local goldStore = Datastore2("Gold", player); local goldStoreData = goldStore:Get()
local armourStore = Datastore2("Armour", player); local armourStoreData = armourStore:Get({ })
local potionsStore = Datastore2("Potions", player); local potionsStoreData = potionsStore:Get({ })
local weaponsStore= Datastore2("Weapons", player); local weaponsStoreData = weaponsStore:Get({ })
local healthStore = Datastore2("Health", player); local healthStoreData = healthStore:Get()
local damageStore = Datastore2("Damage", player); local damageStoreData = damageStore:Get()
local magicStore = Datastore2("MagicDamage", player); local magicStoreData = magicStore:Get()
local levelStore = Datastore2("Level", player); local levelStoreData = levelStore:Get()
local xpStore = Datastore2("Experience", player); local XpStoreData = xpStore:Get()
local maxxpStore = Datastore2("MaxExperience",player); local MaxXpStoreData = maxxpStore:Get()
--// Updates player values with JSON encoded tables
playerArmour.Value = HttpService:JSONEncode(armourStoreData)
playerPotions.Value = HttpService:JSONEncode(potionsStoreData)
playerWeapons.Value = HttpService:JSONEncode(weaponsStoreData)
--// Updates player values with non-table values
playerGold.Value = goldStoreData
playerHealth.Value = healthStoreData
playerDamage.Value = damageStoreData
playerMagicDmg.Value = magicStoreData
playerLevel.Value = levelStoreData
playerXp.Value = XpStoreData
playerMaxXp.Value = MaxXpStoreData
--// Checks to see when data changes and syncs it to DS2
playerGold.Changed:Connect(function(newVal)
goldStore:Set(newVal)
end)
playerArmour.Changed:Connect(function(newVal)
goldStore:Set(newVal)
end)
playerPotions.Changed:Connect(function(newVal)
goldStore:Set(newVal)
end)
playerWeapons.Changed:Connect(function(newVal)
goldStore:Set(newVal)
end)
playerHealth.Changed:Connect(function(newVal)
healthStore:Set(newVal)
end)
playerDamage.Changed:Connect(function(newVal)
damageStore:Set(newVal)
end)
playerMagicDmg.Changed:Connect(function(newVal)
magicStore:Set(newVal)
end)
playerLevel.Changed:Connect(function(newVal)
levelStore:Set(newVal)
end)
playerXp.Changed:Connect(function(newVal)
xpStore:Set(newVal)
end)
playerMaxXp.Changed:Connect(function(newVal)
maxxpStore:Set(newVal)
end)
--// Checks to make sure that everyone has a saveDataFolder
local playerList = game.Players:GetPlayers()
for i = 1, #playerList do
if workspace.Userdata:FindFirstChild(playerList[i].Name.."_Data") == nil then
addPlayerData(playerList[i])
end
end
end
game.Players.PlayerAdded:Connect(addPlayerData)
local function removePlayerData(player)
--// Destroys the player's dataSaveFolder
local dataSaveFolder = workspace.Userdata:FindFirstChild(player.Name.."_Data")
dataSaveFolder:Destroy()
--// Removes any dataSaveFolders that were missed for some reason
local dataFolderList = workspace.Userdata:GetChildren()
for i = 1, #dataFolderList do
if dataFolderList[i]:IsA("Folder") then
if game.Players:FindFirstChild(string.gsub(dataFolderList[i].Name, "_Data", "")) == nil then
dataFolderList[i]:Destroy()
end
end
end
end
game.Players.PlayerRemoving:Connect(removePlayerData)
local function manualAddPlayerData(player)
if workspace.Userdata:FindFirstChild(player.Name.."_Data") == nil then
addPlayerData(player)
end
end
workspace.Userdata.ManualLoadData.OnServerEvent:Connect(manualAddPlayerData)