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

Data Store Problems?

This only loads some of the cash that the player had

local DataStoreService = game:GetService("DataStoreService")
local Money = DataStoreService:GetDataStore("Money") -- 

game.Players.PlayerAdded:connect(function(plr)
	local key = "user_"..plr.userId
	
	local stats = Instance.new("IntValue",plr)
	stats.Name = "leaderstats"
	local level = Instance.new("StringValue",stats)
	level.Name = "Mode"
	level.Value = "Choosing"
	local money = Instance.new("IntValue",stats)
	money.Name = "Cash"		

	money.Value = Money:GetAsync(key) or 0 
	money.Changed:connect(function() Money:SetAsync(key, money.Value) end) 
end)

1 answer

Try using UpdateAsync, and making a function to determine whether or not an old key was saved. Like this:

local DataStoreService = game:GetService'DataStoreService'
local Money = DataStoreService:GetDataStore'Money'
local Players = game:GetService'Players'

local DefaultMoney = 1 -- what they start with

local datalib = {} -- library for data functions

datalib.SaveData = function(store, key, value)
	if store:GetAsync(key) then -- if old, than update
		store:UpdateAsync(key, function()
			return value or DefaultMoney  -- update with this new value (or default)
		end)
	else
		store:SetAsync(key, value) -- if not, make a new store
	end
end

Players.PlayerAdded:connect(function(plr)
	local key = "user_"..plr.userId

	local stats = Instance.new("IntValue",plr)
	stats.Name = "leaderstats"
	local level = Instance.new("StringValue",stats)
	level.Name = "Mode"
	level.Value = "Choosing"
	local money = Instance.new("IntValue",stats)
	money.Name = "Cash"
	money.Value = Money:GetAsync(key) or DefaultMoney 
  
	money.Changed:connect(function()
		datalib.SaveData(Money, key, money.Value)
	end)
end)

3 comments

0

This makes the value 0 every time someone joins.

1

k lemme test it

1

oh, i know why. give me a sec.

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