Saves sometimes just disappear, or changes with players

For some reason, data sometimes just disappear, but for the most part they somehow change with other players, I still can’t understand, I watched datastore for some reason takes someone else’s save.

local datastoreService = game:GetService("DataStoreService")
local Datastore = datastoreService:GetDataStore("valueDataStore_NEW")
local Nezuko2023 = game:GetService("DataStoreService"):GetDataStore("nezukoHalloween2023")
local RankedValues = datastoreService:GetOrderedDataStore("RankedValues")
local valueFolder = game:GetService("ReplicatedStorage").Values

starterValues = {
	Race = "Human";

	Rank = "None";

	Breathing = "None";
	DemonArt = "None";

	TempBreathing = "None";
	TempDemonArt = "None";

	GradientHair = false;
	NewCameraSystem = false;
	Skill1 = "E";
	Skill2 = "R";
	Skill3 = "T";
	Skill4 = "Y";
	Skill5 = "X";
	BlockButton = "F";
	DashButton = "Q";
	BreathingButton = "G";

	BreathingProgression = 4;

	SlayerCorps = false;
	BreathingLvl = 4;
	lvl = 300;
	XP = 0;
	DemonStage = 1;
	BreathingSkin = false;
	BreathingSkins = "";

	MusicEnabled = false;

	Outfit = "None";
	Gender = "";

	DemonColor = 0;
	DemonStageHornType = 0;

	HairColor = 0;
	NichirinColor = 0;
	IdleDemon = "rbxassetid://13960530312";
	IdleSword = "rbxassetid://13953119474";
	CurrentKatana = "Nichirin Sword";
	CrowSkin = "DefaultCrow";
	GradientHairColor = "";

	AccessoryEquip = "nil";

	Yens = 0;
	PlayerBounty = 0;
	LastKilledPlayer = "";

	CandyCorns = 0;

	FlashStep = false;

	UsedCodes = "";

	FunMode = false;
}

local function loadStarterData(player)
	if valueFolder:FindFirstChild(player.Name) then
		valueFolder:Destroy()
	end

	local Values = Instance.new("Folder", valueFolder)
	Values.Name = player.Name

	for name, value in pairs(starterValues) do
		if typeof(value) == "number" then
			local newVal = Instance.new("NumberValue", Values)
			newVal.Name = name
			newVal.Value = value
		elseif typeof(value) == "boolean" then
			local newVal = Instance.new("BoolValue", Values)
			newVal.Name = name
			newVal.Value = value
		elseif typeof(value) == "string" then
			local newVal = Instance.new("StringValue", Values)
			newVal.Name = name
			newVal.Value = value
		end
	end

	local rankedFolder = Instance.new("Folder", game.ReplicatedStorage:WaitForChild("RankedValues"))
	rankedFolder.Name = player.Name
	local NumberValue = Instance.new("NumberValue", rankedFolder)
	NumberValue.Name = "Points"
end

local function saveData(player)
	local Values = valueFolder:FindFirstChild(player.Name)

	local rankedPoints = game:GetService("ReplicatedStorage").RankedValues:FindFirstChild(player.Name).Points
	if not rankedPoints then return end
	if not Values then return end

	local sValues = {}
	local skinValues = {}
	local BreathingSkins = Values.BreathingSkins.Value:split(", ")
	for i, value in pairs(Values:GetChildren()) do
		sValues[value.Name] = value.Value
	end
	for i,v in pairs(BreathingSkins) do
		table.insert(skinValues, v)
	end
	local Success, Error = pcall(function()
		Datastore:SetAsync(player.UserId, sValues)
		Nezuko2023:SetAsync(player.UserId, skinValues)
		RankedValues:SetAsync(player.UserId, rankedPoints.Value)
	end)
	if Error then
		warn("saveData got error: "..Error)
	end
end

local function loadData(player)
	loadStarterData(player)
	local Values = valueFolder:FindFirstChild(player.Name)

	nezukoData = Nezuko2023:GetAsync(player.UserId)
	Data = Datastore:GetAsync(player.UserId)
	rankeddata = RankedValues:GetAsync(player.UserId)

	local function createready()
		local ready = Instance.new("BoolValue")
		ready.Name = "Ready"
		ready.Parent = Values
	end

	if nezukoData then
		Values.BreathingSkins.Value = ""
		for i,v in pairs(nezukoData) do
			if Values.BreathingSkins.Value == "" then Values.BreathingSkins.Value = v end
			if v ~= Values.BreathingSkins.Value then
				Values.BreathingSkins.Value = Values.BreathingSkins.Value..", "..v
			end
		end
	end
	if rankeddata then
		game.ReplicatedStorage:WaitForChild("RankedValues"):WaitForChild(player.Name):WaitForChild("Points").Value = rankeddata
	end
	if Data then
		for name, value in pairs(Data) do
			if Values:FindFirstChild(name) then
				Values[name].Value = value
			end
		end
		createready()
	else
		createready()
	end
end
---------------------------------------

game.Players.PlayerAdded:Connect(function(player)
	coroutine.wrap(loadData)(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(game:GetService("ReplicatedStorage").Values:FindFirstChild(player.Name):GetChildren()) do
		if v.Name == "TempBreathing" or v.Name == "TempDemonArt" then
			v.Value = "None"
		end
		if v.Name == "FunMode" then
			v.Value = false
		end
	end 
	coroutine.wrap(saveData)(player)
	if game:GetService("ReplicatedStorage").Values:FindFirstChild(player.Name) then
		game:GetService("ReplicatedStorage").Values:FindFirstChild(player.Name):Destroy()
	end
end)

game:BindToClose(function()
	for i,player in pairs(game.Players:GetPlayers()) do
		coroutine.wrap(saveData)(player)
	end
end)
2 Likes