I need help with DataStores!

I’m trying to teach myself DataStores from AlvinBlox’s recent Obby Video. But instead of team saving, I’m trying to save IntValues. Please Help!

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("Doesnotd Matterright now")

game.Players.PlayerAdded:Connect(function(player)
	
	local RetrieveData
	
	local success, errorMessage = pcall(function()
		RetrieveData = DataStore:SetAsync(player.UserId.."Days")
		RetrieveData = DataStore:SetAsync(player.UserId.."Hours")
		RetrieveData = DataStore:SetAsync(player.UserId.."Mins")
		RetrieveData = DataStore:SetAsync(player.UserId.."Sec")
	end)
	if success then
		if RetrieveData then
			player:WaitForChild("leaderstats"):WaitForChild("Days").Value = RetrieveData
			player:WaitForChild("Hours").Value = RetrieveData
			player:WaitForChild("Mins").Value = RetrieveData
			player:WaitForChild("Sec").Value = RetrieveData
		else
			
		end
	end
	--player.Days = player:WaitForChild("leaderstats")
end)

game.Players.PlayerRemoving:Connect(function(player)
	local days = player:WaitForChild("leaderstats"):WaitForChild("Days").Value
	local hours = player:WaitForChild("Hours").Value
	local mins = player:WaitForChild("Mins")
	local sec = player:WaitForChild("Sec").Value
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId.."Days",days)
		DataStore:SetAsync(player.UserId.."Hours",hours)
		DataStore:SetAsync(player.UserId.."Mins",mins)
		DataStore:SetAsync(player.UserId.."Sec",sec)
	end)
	if success then
		print("All Saved")
	else
		print(errorMessage)
	end
end)

you’ll have to use it like this, (don’t copy/paste, its just a snippet example)
also you cannot update the same datastore 4 times ina row super fast, it’ll just throttle your request.

--the table you want to save
local someTable = {
        ["Days"] = 0,
        ["Hours"] = 0,
        ["Mins"] = 0,
        ["Sec"] = 0
}

--saving
local success= pcall(function()
    DataStore:SetAsync(tostring(player.UserId), someTable)
end)


--loading
local success, retrievedTable  =pcall(function()
    return DataStore:GetAsync(tostring(player.UserId))
end)

--print values
print[retrievedTable["Days"])
print[retrievedTable["Hours"])
print[retrievedTable["Mins"])
print[retrievedTable["Sec"])

What is RetrievedTable for ???

retrievedtable, is just a temporary table that will store all the values that you grabbed from the players datastore. (remember we saved a table to their datastore, so you’ll have to retrieve it as table too)

so Datastores cant store Number Values?

yes they can store NumberValue.Value but they cannot store NumberValue instances as a whole.

Ohh the IntValue’s Value (I hate the Minimum Text requirement on Dev form)

yes, datastores can only hold String, Number, and Boolean values (and tables that contain these values), BUT they cannot store any ‘instance’
(instances are like Models, Parts, Value Instances, Folders, etc)

You should be storing a dictionary, not 4 keys. Storing 4 keys is not a god idea, and will most probably lead to throttling, You should be storing the key as tostring(plr.UserId) and a dictionary

{days = 0, hours = 0, mins = 0, seconds = 0}

That way, all you would have to do is retrieve is one key, and save only one key.

local retrievedData
local success, err = pcall(function()
	retrievedData = datastore:GetAsync(tostring(plr.UserId))
end)
1 Like