So I’ve been optimizing my game, and I’m trying to store one dictionary in a datastore instead of using several datastores to save the player’s data with the following script. I’m currently testing with an unreleased feature to avoid breaking player data, so that is why cash isn’t saved in a table.
local dataStores = game:GetService("DataStoreService")
local playerData = dataStores:GetDataStore("playerData")
local StarterGui = game:GetService("StarterGui")
local MarketplaceService = game:GetService("MarketplaceService")
local tableData = dataStores:GetDataStore("plrData")
game.ReplicatedStorage.giveCash.OnServerEvent:Connect(function(player, cash)
player.leaderstats.Cash.Value += cash
end)
game.ReplicatedStorage.Events.Start.OnServerEvent:Connect(function(player)
for i, v in pairs(workspace.Bases:GetChildren()) do
if v.Owner.Value == nil then
player.Character.HumanoidRootPart.Position = v.Spawn.Position
player.data.plot.Value = v
end
end
if player.data.plot.Value == nil then
player.data.isJudge = true
player.Character.HumanoidRootPart.Position = workspace.judgeArea.Spawn.Position
end
workspace:SetAttribute("gameInProgress", true)
end)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
Cash.Parent = leaderstats
Cash.Value = playerData:GetAsync("Player_" .. player.UserId)
workspace.playerCount.Value += 1
local data = Instance.new("Folder", player)
data.Name = "data"
local plot = Instance.new("ObjectValue", data)
plot.Name = "plot"
local isJudge = Instance.new("BoolValue", data)
isJudge.Name = "isJudge"
local membershipTime = Instance.new("StringValue", data)
membershipTime.Name = "membershipTime"
local datastoredata = player.UserId or "no data"
print(datastoredata)
local membership = datastoredata[1]
membershipTime.Value = membership or "not member"
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
local cash = player.leaderstats.Cash.Value
playerData:SetAsync("Player_" .. player.UserId, cash)
end)
if success then
print("Saved " .. player.leaderstats.Cash.Value)
elseif err then
error(err)
end
local data = {
isMember = player.data.membershipTime
}
tableData:SetAsync(player.UserId, data)
workspace.playerCount.Value -= 1
end)
local Players = game:GetService('Players')
local ServerScriptService = game:GetService('ServerScriptService')
local ChatService = require(ServerScriptService:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))
ChatService.SpeakerAdded:Connect(function(PlayerName)
local Speaker = ChatService:GetSpeaker(PlayerName)
Speaker:SetExtraData('NameColor', Color3.fromRGB(209, 193, 97))
Speaker:SetExtraData('ChatColor', Color3.fromRGB(178, 248, 229))
Speaker:SetExtraData('Tags', {{TagText = 'Beta Tester', TagColor = Color3.fromRGB(189, 83, 83)}})
end)
game:BindToClose(function()
if game["Run Service"].IsStudio then
wait(3)
end
end)
However, when I save the data, it errors
and if i’m not mistaken, they automatically run JSONEncode and Decode. And I have tried running the JSON myself, but that returns null