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

DataStore not working?[unsolved]

datastore = game:GetService("DataStoreService"):GetDataStore("FarmStatsCJ")

local function set(key,value)
		datastore:SetAsync(key,value)
end

local function get(key)
	datastore:GetAsync(key)
end

game.Players.PlayerRemoving:connect(function(player)
	wait(1)
	local l = player.leaderstats
	set('leaderstatsFarmCJ1'..player.userId,l.Money)
	set('leaderstatsFarmCJ2'..player.userId,l.level)
	set('leaderstatsFarmCJ3'..player.userId,l.Harvests)
	set('valsFarmCJ1'..player.userId,player.storage.ex.Value)
	set('valsFarmCJ2'..player.userId,player.storage.exm.Value)
	set('valsFarmCJ3'..player.userId,player.storage.waittime.Value)
	set('valsFarmCJ4'..player.userId,player.storage.code.Value)
	set('inventoryFarmCJ1'..player.userId,player.inventory.Carrots.Value)
	set('inventoryFarmCJ2'..player.userId,player.inventory.Wheat.Value)
	set('inventoryFarmCJ3'..player.userId,player.inventory.Corn.Value)
end)

game.Players.PlayerAdded:connect(function(play)
	wait(2)
	local l = play.leaderstats
	local i = play.inventory
	local s = play.storage
	if l.Money then
		l.Money.Value = get('leaderstatsFarmCJ1'..play.userId)
		l.level.Value = get('leaderstatsFarmCJ2'..play.userId)
		l.Harvests.Value = get('leaderstatsFarmCJ3'..play.userId)
		s.ex.Value = get('valsFarmCJ1'..play.userId)
		s.exm.Value = get('valsFarmCJ2'..play.userId)
		s.waittime.Value = get('valsFarmCJ3'..play.userId)
		i.Carrots.Value = get('inventoryFarmCJ1'..play.userId)
		i.Wheat.Value = get('inventoryFarmCJ2'..play.userId)
		i.Corn.Value = get('inventoryFarmCJ3'..play.userId)
	end
end)

The datastore doesn't work, and since I've never used datastore before, I don't know what i did wrong.

1 answer

✓ Accepted answer
  1. What if the player doesn't have saved data yet? Doing get('idk') would return nil. Doing Money.Value = nil isn't very good.

  2. Why don't you make the leaderstats/inv/... in that script too? Then you don't have to wait.

  3. Why do you wait(1) on PlayerRemoving? The server might shutdown before you can save.

  4. Instead of saving every stat in a separate key, why not put it all in one table?

-- Saving
local stats = {}
stats.Money = Money.Value
stats.Role = Role.Value
set("Data_"..play.userId,stats)
-- Loading
local stats = get("Data_"..play.userId)
if not stats then return end -- NO STATS, USE DEFAULT
Money.Value = stats.Money
Role.Value = stats.Role

1 comment

0

thank you. the consept of what this is helped me

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