- What do you want to achieve?
I want to make a datastore system for my simulator.
- What is the issue?
I don’t know if I should be this table which is spread out like a normal table on the server.
local sessionData = {
Spirits = 0,
Electro = false,
Vine = false,
Violet = false,
Razor = false,
Negative = false
}
-
What solutions have you tried so far?
I’ve asked the AI assistant on Roblox but I still feel uncertain.
Server-Side Datastoring
Players.PlayerAdded:Connect(function(player)
playerStatsModule:Join(player,sessionData)
end)
Players.PlayerRemoving:Connect(function(player)
playerStatsModule:Leave(player,sessionData)
end)
game:BindToClose(function()
task.wait(1)
end)
PlayerStats Module
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SpiritSimulator")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Levels = ReplicatedStorage.Resources.Levels
local dataLoader = require(script.Parent.DataLoader)
local PlayerStats = {}
--Initializing data
function PlayerStats:Join(player,sessionData)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local spirits = Instance.new("IntValue")
spirits.Name = "Spirits"
leaderstats.Parent = player
spirits.Parent = leaderstats
local success, result = pcall(function()
return DataStore:GetAsync(tostring(player.UserId))
end)
if success then
spirits.Value = result.Spirits
local particlePack = dataLoader:Base(Levels,sessionData)
print(particlePack)
if particlePack == nil then return end
print("Success")
self:CloneComponents(particlePack,player)
elseif result then
warn(result)
end
end
function PlayerStats:Leave(player,sessionData)
self:SetData(player,sessionData)
local success, result = pcall(function()
DataStore:UpdateAsync(tostring(player.UserId),function(pastData)
print(sessionData)
pastData = sessionData
return pastData
end)
end)
end
function PlayerStats:Find(player)
return player.Character.HumanoidRootPart.RootAttachment:FindFirstChildWhichIsA("ParticleEmitter")
end
function PlayerStats:Initialize(sessionData,particle)
for key,value in pairs(sessionData) do
if key ~= "Spirits" and particle.Name == key then
return key
end
end
end
function PlayerStats:SetData(player,sessionData)
sessionData.Spirits = player.leaderstats.Spirits.Value
local particle = self:Find(player)
if particle == nil then return end
local init = self:Initialize(sessionData,particle)
sessionData[init] = true
end
--Loading
function PlayerStats:CloneComponents(particlePack,player)
for _,particle in particlePack:GetChildren() do
local newParticle = particle:Clone()
newParticle.Parent = player.Character.HumanoidRootPart.RootAttachment
end
end
return PlayerStats
DataLoader Module
function DataLoader:FindData(sessionData)
for key,value in pairs(sessionData) do
print(key,value)
if value == true and key ~= "Spirits" then
return key
end
end
end
function DataLoader:Levels(levels,key)
for _,comp in ipairs(levels:GetDescendants()) do
if comp.Name == "Key" and comp:IsA("ParticleEmitter") then
return comp.Parent
end
end
end
function DataLoader:Base(levels,sessionData)
local key = self:FindData(sessionData)
local level = self:Levels(levels,key)
return level
end
There is a lot of datastoring code but to sum it up I’m not certain how or if I should use tables.