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

how would I save a table to a datastore?

I am trying to store values such as coins in a table and then save the table. I have the values constantly being updated and pcall functions. Whenever I run the script I get the default value of coins and the pcall function error.

--Get The Service's
local replicatedStorage = game:GetService("ReplicatedStorage")
local timePlayedValue = replicatedStorage.PlayerStats:WaitForChild("TimePlayedValue")
local totalTimeValue = replicatedStorage.PlayerStats:WaitForChild("TotalTimeValue")
local httpsService = game:GetService("HttpService")
local dsService = game:GetService("DataStoreService")
local mainStore = dsService:GetDataStore("MainDataStore")

--Get The Gameplay Stats
local CompletedMissionsValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("CompletedMissionsValue")
local FailedMissionsValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("FailedMissionsValue")
local PreviousMissionValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("PreviousMissionValue")
local TimesDiedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TimesDiedValue")
local TrapsAvoidedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TrapsAvoidedValue")
local TrapsFailedValue = game:GetService("ReplicatedStorage").GamePlayStats:WaitForChild("TrapsFailedValue")

--Get The PlayerStats
local CoinsValue = replicatedStorage.PlayerStats:WaitForChild("Coins")
local DateFirstJoinedValue = replicatedStorage.PlayerStats:WaitForChild("DateFirstJoinedValue")
local RankValue = replicatedStorage.PlayerStats:WaitForChild("Rank")
local ExperienceValue = replicatedStorage.PlayerStats:WaitForChild("XP")

--Table To Store Data
local AllStatsTable = {CompletedMissionsValue,FailedMissionsValue,PreviousMissionValue,TimesDiedValue,TrapsAvoidedValue,TrapsFailedValue,CoinsValue,
	DateFirstJoinedValue,RankValue,ExperienceValue,totalTimeValue}


--Increment Time Values
incrementor = coroutine.create(function()
	while wait(1) do
		timePlayedValue.Value = timePlayedValue.Value + 1
		totalTimeValue.Value = totalTimeValue.Value + 1
	end
end)

coroutine.resume(incrementor)

--FIX DATASTORE
game:GetService("Players").PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		mainStore:SetAsync(player.UserId,AllStatsTable)
	end)
	if err then
		print("error saving")
	end
end)

game:GetService("Players").PlayerAdded:Connect(function(player)
local success, err = pcall(function()
		local data = mainStore:GetAsync(player.UserId)
		if data then
			print(data[7])
		else
			print("no data")
		end
	end)
	if err then
		print("error saving")
	end
end)

1 answer

✓ Accepted answer

Lol, the answer is legit on this website luckily, https://scriptinghelpers.org/guides/saving-and-loading-data-with-datastores thisl ink will tell you basically everything you need to know abt DataStore Service and stuff but, make sure to look at the documentation from the Developer Roblox page also. https://developer.roblox.com/en-us/articles/Saving-Player-Data

4 comments

0

its blocked and you didn't answer my question

0

First, get the player with it's key basically like local Key = "KeyHere|"..player.UserId then, you make a table with the values like table = {lol = 20, lol2 = 200} finally, you take the table and data and use DATASTORE NAME:SetAsync(Key, table) to save the data

0

Sorry about that I didn't know it was blocked

0

thanks soo much! That fixed it! :)

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