Datastores being broken

You can still do that by the way, I do it in my game, the actual data is stored in tables, whenever a player joins, a ServerScript gets that data through a ModuleScript and sets the values in the player to the values of the data. Whenever I want to update the data, all I have to do is fire a BindableEvent and that will just set the values in the player to the values of the data.

Updated code that supports abilities:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local isStudio = RunService:IsStudio()
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("LeaderStatSave")

local temporalData = {}
local abilities = {
	"Boost",
	"Heal",
	"Engineer"
}

Players.PlayerAdded:Connect(function(plr)
	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"

	local private = Instance.new("Folder")
	private.Name = "PrivateStats"

	local Coins = Instance.new("NumberValue", leader)
	Coins.Name = "Coins"

	local Wins = Instance.new("NumberValue", leader)
	Wins.Name = "Wins"

	leader.Parent = plr

	temporalData[plr.UserId] = ds:GetAsync(plr.UserId) or {}

	if temporalData[plr.UserId].Abilities == nil then
		temporalData[plr.UserId].Abilities = {}
	end

	for _, ability in ipairs(abilities) do
		if temporalData[plr.UserId].Abilities[ability] == nil then
			temporalData[plr.UserId].Abilities[ability] = false
		end
		local bool = Instance.new("BoolValue")
		bool.Name = ability
		bool.Value = temporalData[plr.UserId].Abilities[ability]
		bool:GetPropertyChangedSignal("Value"):Connect(function()
			temporalData[plr.UserId].Abilities[ability] = bool.Value
		end)
		bool.Parent = private
	end

	private.Parent = plr

	Coins.Value = temporalData[plr.UserId].Coins or 0
	Coins:GetPropertyChangedSignal("Value"):Connect(function()
		temporalData[plr.UserId].Coins = Coins.Value
	end)

	Wins.Value = temporalData[plr.UserId].Wins or 0
	Wins:GetPropertyChangedSignal("Value"):Connect(function()
		temporalData[plr.UserId].Wins = Wins.Value
	end)

	plr.AncestryChanged:Connect(function()
		if plr:IsDescendantOf(game) or temporalData[plr.UserId] == nil then
			return
		end
		if isStudio then
			print("Data not saved because it runs in Studio")
			return
		end
		local s, err = pcall(function()
			ds:UpdateAsync(plr.UserId, function()
				return temporalData[plr.UserId]
			end)
		end)
		if s ~= true then
			warn("Failed saving data for player: " .. plr.Name .. ", key: " .. plr.UserId .. "\n" .. tostring(err))
		else
			print("Data saved for player: " .. plr.Name)
		end
		temporalData[plr.UserId] = nil
	end)
end)
2 Likes

Just know, i am not a great scripter but this is extremely confusing, i;ve never used module scripts in my life so yeah.

Thanks! Do i just add more after the heal engineer etc etc?

1 Like

Just a question, what is the point? Wouldn’t this always be true?

Thanks but could i make it so its inside the player as my scripts require that
example, can i do

    private.Parent = plr

Edit: it’s there, my bad.

This is absolutely more reliable, when player leaves they no longer exist in game that’s why

So couldn’t you just do if plr?

image

Your data should be set to table, try resetting your data

game:GetService("DataStoreService"):GetDataStore("LeaderStatSave"):RemoveAsync(your user id)
1 Like

Sorry if this has already been mentioned, I’m on my phone. Have you considered using a community resource such as ProfileService? Resources like this are great at hiding all the complexities of data storage and providing you the necessary functions. They work on the concept of ‘promise’ to save data.

I think i should do this in console, am i right?

1 Like

I do not know whats a “ProfileService” but i guess i will learn.

Here is the thread. Sorry for the tangent, hope you solve your problem!

FINALLY! IT WORKS! THANK YOU! ABSOLUTE LEGEND. But if you would mind, why was my previous data breaking the datastore?

You’re getting multiple data stores to save one value, that’s why you should use tables
Also I suggest using pcall to catch errors and prevent data from failing to save completely

1 Like

Thanks! And thanks again for being patient.

1 Like

It broke again :disappointed_relieved:
Heres the error:
image
image

Nevermind, it’s just being very weird. Ill make a new post about it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.