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

How do I make this saving system I created work?

I have a saving system in my game. It will not save any stats. Please try to help me. This is the code -

local dataserv = game:GetService("DataStoreService")
local diamondsdata = dataserv:GetDataStore("DiamondsSaveData")
local moneydata = dataserv:GetDataStore("MoneySaveData")
local multidata = dataserv:GetDataStore("MultiplierSaveData")
local rebirthdata = dataserv:GetDataStore("RebirthSaveData")
local xpdata = dataserv:GetDataStore("ExperienceSaveData")
game.Players.PlayerAdded:Connect(function(p)
local stats = Instance.new("Folder")
stats.Name = "stats"
stats.Parent = p

local diamonds = Instance.new("NumberValue")
diamonds.Name = "Diamonds"
diamonds.Value = diamondsdata:GetAsync(p.UserId) or 0
diamonds.Parent = stats

local money = Instance.new("NumberValue")
money.Name = "Money"
money.Value = moneydata:GetAsync(p.UserId) or 0
money.Parent = stats


local multi = Instance.new("NumberValue")
multi.Name = "Multiplier"
multi.Value = multidata:GetAsync(p.UserId) or 1
multi.Parent = stats


local rebirth = Instance.new("NumberValue")
rebirth.Name = "Rebirth"
rebirth.Value = rebirthdata:GetAsync(p.UserId) or 1
rebirth.Parent = stats


local xp = Instance.new("NumberValue")
xp.Name = "Experience"
xp.Value = xpdata:GetAsync(p.UserId) or 0
xp.Parent = stats
game.Players.PlayerRemoving:Connect(function()
	diamondsdata:SetAsync(p.UserId)
	moneydata:SetAsync(p.UserId)
	multidata:SetAsync(p.UserId)
	rebirthdata:SetAsync(p.UserId)
	xpdata:SetAsync(p.UserId)
end)
end)


2 answers

from line 40 to 44, you just entered the first argument (the player's userId). You have to insert the 2nd argument, which is the value you're gonna save in:

game.Players.PlayerRemoving:Connect(function(player)
	diamondsdata:SetAsync(p.UserId, player.stats.Diamonds.Value)
	moneydata:SetAsync(p.UserId, player.stats.Money.Value)
	multidata:SetAsync(p.UserId, player.stats.Multiplier.Value)
	rebirthdata:SetAsync(p.UserId, player.stats.Rebirth.Value)
	xpdata:SetAsync(p.UserId, player.stats.Experience.Value)
end)

replace your lines 39 to 45 by the script above

3 comments

0

@TheRealPotatoChips that script does not work

0

you have to publish your game and enable API services to use datastore

0

I did that quite a long time ago.

I would recommend creating a module that handles all data store related stuff, this approach allows for a uniform way of handling data store failures appropriately.. as of right now, you code creates multiple game datastore folders via DatastoreService:GetDataStore()... in all of your datastore related code, it's most elegant for this method to be called only once.. so write a data store handle like this:

local gameDataStoreKey = "MYGAMEDATASTOREKEY87378" 
local DatastoreService = game:GetService("DataStoreService");

local ds = {};

function ds.init(scope)
	local Datastore = DataStoreService:GetDataStore(gameDataStoreKey, scope)
	
	local accessObject = {}
	function accessObject.getData(player, generalKey)
		local key = player.UserId..generalKey
		local data = nil
		local s, e = pcall(function()
			data = Datastore:GetAsync(key)
		end)

		if(not s) then
			error(e);
		else
			return data
		end
	end

	function accessObject.setData(player, generalKey, value)
		local key = player.UserId..generalKey
		local s, e = pcall(function()
			 Datastore:SetAsync(key, value)
		end)
		
		if(not s) then
			error(e);
		end)
	end
end);

return ds

now with that you can simply do something like this:

local stats= require(directory_of_the_above_module).init("playerLeaderstats")

Players.PlayerAdded:Connect(function(player)
	local data = stats.getData(player, "Coins") or 0
	print(player.Name,"has ",data,"Coins")
end)

Players.PlayerRemoving:Connect(function(player)
	local coins = player.leaderstats.Coins.Value
	stats.setData(player, "Coins", coins)
end)

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