Hi, i made a data store script, and i stuck with a problem that i cant really modify values of StringValues
runtime(because it will not replicate and just change nothing, this will not even save).
So i want to fix this problem. There is code:
local service = {}
local profileStore = require(script.Parent.ProfileStore)
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local profileTemplate = require(script.ProfileTemplate)
local dataManager = require(script.Manager)
local xpnew = Instance.new
service.GetToken = function()
return runService:IsStudio() and "Data-Test" or "PlayerDataStore"
end
local PlayerStore = profileStore.New(service.GetToken(), profileTemplate)
local Profiles: {[player]: typeof(PlayerStore:StartSessionAsync())} = {}
local function CreateData(player : Player, Profile: typeof(PlayerStore:StartSessionAsync()))
local config = xpnew("Folder", player.Character)
config.Name = "Config"
local armor = xpnew("Folder", config)
armor.Name = "Armor"
local armorPenetration = xpnew("StringValue", armor)
armorPenetration.Name = "ArmorPenetration"
armorPenetration.Value = Profile.Data.ArmorPenetration
if config then
print("Data for player is created/restored.")
end
end
local function PlayerAdded(player : Player)
local profile = PlayerStore:StartSessionAsync(`{player.UserId}`, {
Cancel = function()
return player.Parent ~= players
end,
})
if profile ~= nil then
profile:AddUserId(player.UserId)
profile:Reconcile()
profile.OnSessionEnd:Connect(function()
dataManager.Profiles[player] = nil
player:Kick(`Profile session end - Please rejoin`)
end)
if player.Parent == players then
dataManager.Profiles[player] = profile
CreateData(player, profile)
else
profile:EndSession()
end
else
player:Kick(`Profile load fail - Please rejoin`)
end
end
service.Connect = function()
for _, player in players:GetPlayers() do
task.spawn(PlayerAdded, player)
end
players.PlayerAdded:Connect(PlayerAdded)
players.PlayerRemoving:Connect(function(player : Player)
local profile = dataManager.Profiles[player]
if profile ~= nil then
profile:EndSession()
end
player.CharacterAdded:Connect(function (c)
c.Parent = workspace
end)
end)
end
service.Connect()
return service
I think that i can just make remote events for updating each value and Firing client. But i dont really think that this is efficient way.