-
This has been my 3rd post(in this devforum & about DataStore2), the title topic is self-explanatory.
-
For a little context, when I try copying data variable and when I put
ExampleVariable.Value = datait doesn’t change theExampleVariablefor my problem. -
Extra Context: This are created in the
game:GetService('ServerStorage')and the Cash is in player -
There were no errors or anything but the tables that were printed shows the values but not editing in the player.
Script:
-- Data Var:
local dss: DataStoreService = game:GetService('DataStoreService')
local ds: DataStore = dss:GetDataStore('ClientData')
-- Players:
local plrs: Players = game:GetService('Players')
-- Function:
-- Duplicated DataFolder to Player
local function DuplicatedData(plr: Player)
local DataFolder: Folder = game:GetService('ServerStorage').DataFolder -- Main DataFolder
local DataStorageFolders: Folder = DataFolder:GetChildren()
for index, folder in DataStorageFolders do
for _, values in folder:GetChildren() do
local dupfolder = folder:Clone()
dupfolder.Parent = plr
end
end
end
-- On Player Joining --
local function OnLoadClientData(plr: Player)
local data = nil
local playerKey = plr.UserId -- key
local success, err = pcall(function()
data = ds:GetAsync(playerKey)
end)
local KickMessage = 'Server could not load your data'
if not success then plr:Kick(KickMessage) return end
DuplicatedData(plr)
local DataFolder: Folder = plr:WaitForChild('1')
local Values: IntValue = DataFolder:GetChildren()
for _, v in Values do
for i, value in data do
Values.Value = value
end
end
print('Loaded: ', data)
end
-- On Player Leaving --
local function OnSaveClientData(plr: Player)
local data = nil
local playerKey = plr.UserId -- key
local DataFolder: Folder = plr:WaitForChild('1')
local dataTab = {
DataFolder:WaitForChild('Cash').Value
}
local success, err = pcall(function()
data = ds:SetAsync(playerKey, dataTab)
end)
if success then
print('Saved: ', dataTab)
else
warn('Server Could Not Save Data')
end
end
plrs.PlayerAdded:Connect(OnLoadClientData) -- Load Data
plrs.PlayerRemoving:Connect(OnSaveClientData) -- Save Data