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

I have tried lots of different saving scripts, and none of them work, can someone fix this one?

This one I was trying to save multiple values in one, but its not working. I have also tried using it for one, it gave no errors but didnt work. Does anyone know one that will work or can fix this one? And it also says im giving the datastore to many requests.

local DSService = game:GetService('DataStoreService'):GetDataStore('Protectiveses')
game.Players.PlayerAdded:connect(function(plr)
	--Define varibles
	local uniquekey= 'id'..plr.userId
	local leaderstats = Instance.new('IntValue',plr)
	local savevalue = Instance.new('IntValue')
	local savevalue2 = Instance.new('IntValue')
	leaderstats.Name = 'leaderstats'
	savevalue.Parent = leaderstats
	savevalue.Name = 'Money'
	savevalue2.Parent = leaderstats
	savevalue2.Name = 'Level'
	savevalue2.Value = 1
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	local GetSaved2 = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		savevalue2.Value = GetSaved2[1]
	else
		local NumbersForSaving = {savevalue.Value}
		DSService:SetAsync(uniquekey,NumbersForSaving)
		local NumbersForSaving2 = {savevalue2.Value}
		DSService:SetAsync(uniquekey,NumbersForSaving2)
		
		game.ServerStorage.Give.Event:Connect(function(Amount)
		savevalue.Value = savevalue.Value + Amount
		game.ServerStorage.Give.Event:Connect(function(Amount)
		savevalue2.Value = savevalue2.Value + Amount
	  end)
	end)
  end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.leaderstats.Money.Value}
	DSService:SetAsync(uniquekey, Savetable)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.leaderstats.Level.Value}
	DSService:SetAsync(uniquekey, Savetable)
end)

2 comments

0

My answer will work

0

Stolen from alvinbloxx: https://www.youtube.com/watch?v=tOOS9lMIYes + only requesting a fix

1 answer

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SaveStats")

local function Load(plr)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = stats
	
	local bux = Instance.new("IntValue")
	bux.Name = "Bux"
	bux.Parent = stats
	
	local key = "plr-"..plr.UserId
	local saved
	
	local success, err = pcall(function()
		saved = DataStore:GetAsync(key)
	end)
	
	if not success then
		warn("Failed to read data"..tostring(err))
		return
	end
	
	if saved then
		coins.Value = saved.Coins
		bux.Value = saved.Bux
	else
		local statsToBeOverwritten = {
			["Coins"] = coins.Value;
			["Bux"] = bux.Value
		}
		
		local succes, er = pcall(function()
			DataStore:SetAsync(key, statsToBeOverwritten)
		end)
		
		if not succes then
			warn("Failed to overwrite data"..tostring(er))
			return
		end
	end
end

local function Save(plr)
	local key = "plr-"..plr.UserId
	
	local statsToBeOverwritten = {
		["Coins"] = plr.leaderstats.Coins.Value;
		["Bux"] = plr.leaderstats.Bux.Value
	}
	
	local success, err = pcall(function()
		DataStore:SetAsync(key, statsToBeOverwritten)
	end)
	
	if not success then
		warn("Failed to overwrite data"..tostring(err))
		return
	end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

Untested

1 comment

0

Make sure you have data stores enabled for use in servers and studio

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