Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't my DataStore save data?

It is as it says, I need to know why my datastore is not saving data. I've tried many times to fix this but I am not very good with datastore so I need some help with this.

Note: It creates data for the player but then it does not save.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore = DataStoreService:GetDataStore("Store")

--// Settings \\--
local StarterData = {
	Coins = 0;
	Knowledge = 0;
	Multiplier = 1;
	Capacity = 10;
	BooksUnlocked = 1;
	BookEquipped = 0;
	BooksBought = 0;
}
local playersavetable = {}
local AUTO_SAVE_INTERVAL = 180;

--// Functions \\--
local function loadStarterData(Player)
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = Player
	local NotShowing = Instance.new("Folder")
	NotShowing.Name = "NotShowing"
	NotShowing.Parent = Stats
	for statname, statvalue in pairs(StarterData) do
		
		if statname == "Capacity" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = NotShowing
			
		elseif statname == "Multiplier" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = NotShowing
			
		elseif statname == "BooksUnlocked" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = NotShowing	
			
		elseif statname == "BookEquipped" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = NotShowing	
			
		elseif statname == "BooksBought" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = NotShowing	
			
		elseif type(statvalue) == "number" then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Stats
			
		elseif type(statvalue) == "boolean" then
			local boolvalue = Instance.new("BoolValue")
			boolvalue.Name = statname
			boolvalue.Value = statvalue
			boolvalue.Parent = NotShowing
			
		elseif type(statvalue) == "string" then
			local stringvalue = Instance.new("StringValue")
			stringvalue.Name = statname
			stringvalue.Value = statvalue
			stringvalue.Parent = NotShowing
		end
	end
end

local function loadData(Player)
	local Data
	local s, e = pcall(function()
		Data = DataStore:GetAsync("UserId:"..Player.UserId)
	end)
	
	if s then
		print("Getting "..Player.Name.."'s data was successful.")
	else
		warn("Something went wrong when loading "..Player.Name.."'s data.")
	end
	
	if Data then
		for statname, statvalue in pairs(Data) do
			Player.leaderstats[statname].Value = statvalue
		end
		print(Player.Name.."'s data has been loaded.")
	else
		print(Player.Name.." has no data! Generating new data.")
	end
end

local function saveData(Player)
	if RunService:IsStudio() then return end
	local Data = {}
	for _, stat in pairs(Player.leaderstats:GetChildren()) do
		Data[stat.Name] = stat.Value
	end
	local s, e = pcall(function()
		DataStore:SetAsync("UserId"..Player.UserId, Data)
	end)
	if s then
		print(Player.Name.."'s data has been successfully saved.")
	else
		warn("Something went wrong while saving "..Player.Name.."'s data.")
	end
end

--
--
--

Players.PlayerAdded:Connect(function(Player)
	playersavetable[Player] = tick()
	loadStarterData(Player)
	loadData(Player)
end)

Players.PlayerRemoving:Connect(function(Player)
	saveData(Player)
end)

while true do
	wait(1)
	for _, Player in pairs(Players:GetPlayers()) do
		if tick() - playersavetable[Player] >= AUTO_SAVE_INTERVAL then
			saveData(Player)
			playersavetable[Player] = tick()
		end
	end
end

1 comment

0

One thing is the max leader stats you can have is 4.

1 answer

If this explains, I don’t know

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")
local ds2 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local gems = Instance.new("IntValue", folder)
 gems.Name = "Gems"
 local cash = Instance.new("IntValue", folder)
 cash.Name = "Cash"
 
 gems.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, gems.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)
 
 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)
 
 cash.Changed:connect(function()
  ds2:SetAsync(plr.UserId, cash.Value)
 end)
end)

I hope this explains what you need

1 comment

0

That is not what I am trying to do, I am trying to make it save, I know that it is possible to save the stats because of things i've done in the past. This one is new and I can't quite figure out what to do.

This discussion is preserved as it was. New answers, comments, and votes are closed.