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

Is this Data Store likely to fail?

local Stats = {"Stat1","Stat2","Stat3","Stat4","Stat5"
,"Stat6","Stat7","Stat8","Stat9","Stat10"}

local function Create(User)
	local StatsFolder = Instance.new("Folder",User)
	StatsFolder.Name = "leaderstats"
	for i = 1,#Stats do
		Instance.new("IntValue",StatsFolder).Name = Stats[i]
	end
end

game.Players.PlayerAdded:connect(function(Player)
	Create(Player)
end)

This script creates the stats

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(Player)
	Player:WaitForDataReady()
local Stats = Player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #Stats do
		DataStore:SetAsync(Stats[i].Name,Stats[i].Value)
	end
end)

game.Players.PlayerAdded:connect(function(Player)
	Player:WaitForDataReady()
local Stats2 = Player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #Stats2 do
		Stats2[i].Value = DataStore:GetAsync(Stats2[i].Name)
	end
end)

This script saves the stats

I don't want my players to work hard and then lose all their data when they leave, is this system likely to fail?

2 comments

1

I highly recommend this video https://www.youtube.com/watch?v=tOOS9lMIYes

0

Thank you for sharing this video

1 answer

✓ Accepted answer

Your script currently will not work as you intend it due to the keys used. In addition to this you do not manage any of the potential errors with a data store (see limitations).

Side note, you do not use Player:WaitForDataReady() as this is for Data persistence not a data store. A data store does not require a player to store any data.

They keys used are fixed meaning that the data will be shared for all players, this is a simple fix as it only requires you to make the key unique for each player by adding in their UserId to the key.

You should also check the wiki on the limitations of a data store

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