DataStore loading previous players data to new player

Players.PlayerAdded:Connect(function(plr)
	local Data = table.clone(DataFormat)
	local success, err = pcall(function()
		Data = DataStore:GetAsync(plr.UserId)
	end)
	if Data then
		LoadData(plr, Data)
		Gamepasses(plr)
		Multipliers(plr)
		AdditionalInfo(plr)
	else
		Data = table.clone(DataFormat)
		LoadData(plr, Data)
		Gamepasses(plr)
		Multipliers(plr)
		AdditionalInfo(plr)
		warn("Data didn't load.")
	end
	plr.CharacterAdded:Connect(function(char)
		Addtag(plr)

		plr.Character.Humanoid.WalkSpeed = 25
		game.ReplicatedStorage.Trails.AddTrailServer:Fire(plr)
	end)
end)

This is my code and I have a problem.

Whenever a new player joins the game with no saved data, it loads data from the previous player with data. I have looked at this for a while and can’t see why and I feel like it is very oblivious. Any help will be greatly appreciated.

do you see the warning in the output?

No, it does not output the warning.

this is because the Data variable is never False

There are too many functions we cannot see here.

1 Like
Players.PlayerAdded:Connect(function(plr)
	local Data = table.clone(DataFormat)
	local success, err = pcall(function()
		Data = DataStore:GetAsync(plr.UserId)
	end)
	if success then
		LoadData(plr, Data)
		Gamepasses(plr)
		Multipliers(plr)
		AdditionalInfo(plr)
	else
		Data = table.clone(DataFormat)
		LoadData(plr, Data)
		Gamepasses(plr)
		Multipliers(plr)
		AdditionalInfo(plr)
		warn("Data didn't load.")
	end
	plr.CharacterAdded:Connect(function(char)
		Addtag(plr)

		plr.Character.Humanoid.WalkSpeed = 25
		game.ReplicatedStorage.Trails.AddTrailServer:Fire(plr)
	end)
end)

try this

Alright I will try this, give me a moment.

This now gives the player no data at all.

Players.PlayerAdded:Connect(function(plr)
	local Data = nil
	local success, err = pcall(function()
		Data = DataStore:GetAsync(plr.UserId)
	end)
	if Data ~= nil then
		LoadData(plr, Data)
		Gamepasses(plr)
		Multipliers(plr)
		AdditionalInfo(plr)
	else
		Data = table.clone(DataFormat)
		LoadData(plr, Data)
		Gamepasses(plr)
		Multipliers(plr)
		AdditionalInfo(plr)
		warn("Data didn't load.")
	end
	plr.CharacterAdded:Connect(function(char)
		Addtag(plr)

		plr.Character.Humanoid.WalkSpeed = 25
		game.ReplicatedStorage.Trails.AddTrailServer:Fire(plr)
	end)
end)

try this

It now gives the player the previous player to joins data. This is very confusing…

do you see the warning? in the output

No, the warning is not outputted again.

can you put a print(Data) just before if Data ~= nil…

This outputs on studio but not when I am playing in the game as it doesn’t show in the developer console.

When I am in studio I can’t test it because there has not been a previous player.

i think there is a problem with the functions, cause i dont see any problem in this code

local function LoadData(plr : Player, data)
	local NewDataFormat = table.clone(DataFormat)
	for i, v in pairs(NewDataFormat) do
		local Folder = Instance.new("Folder", plr)
		Folder.Name = i
		for a,b in pairs(v) do
			if typeof(b) == "boolean" then
				local Value = Instance.new("BoolValue", Folder)
				Value.Name = a
				Value.Value = data[i][a] or NewDataFormat[i][a]
			elseif typeof(b) == "string" then
				local Value = Instance.new("StringValue", Folder)
				Value.Name = a
				Value.Value = data[i][a] or NewDataFormat[i][a]
			elseif typeof(b) == "number" then
				local Value = Instance.new("IntValue", Folder)
				Value.Name = a
				Value.Value = data[i][a] or NewDataFormat[i][a]
			end
		end
	end
	print(data)
end

Quick question, why are you doing this?

can you send the DataFormat?

typing this for min character limit

It looks more nice than the original Roblox nametag.

local Settings = require(game.ReplicatedStorage.Information)
local DataFormat = Settings.DataSave
Information.DataSave = {
	["leaderstats"] = {
		["Wins"] = 0,
		["Points"] = 0,
	},

	["PlrInfo"] = {
		["CurrentTrail"] = "",
		["Spins"] = 1,
		["LastTime"] = 0,
		["StarterPack"] = false,
	},

	["Gears"] = {
		["Cake"] = false,
		["Cheez burger"] = false,
		["Pizza"] = false,
		["Hippo"] = false,
		["Monkey"] = false,
		["Panda Plushie"] = false,
		["Slingshot"] = false,
		["GrappleHook"] = false,
		["BloxyCola"] = false,
	}
}